1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| import os
def convert(root, target_filePath, from_encode='gbk', to_encode='utf-8'): if os.path.exists(root) and os.path.exists(target_filePath): if os.path.isabs(root): list_dir = os.listdir(root) for l in list_dir: if os.path.isdir(os.path.join(root, l)): if not os.path.exists(os.path.join(target_filePath, l)): os.mkdir(os.path.join(target_filePath, l)) convert(os.path.join(root, l), os.path.join(target_filePath, l), from_encode, to_encode) else: with open(os.path.join(target_filePath, l), 'w') as f: for i in open(os.path.join(root, l), 'r'): f.write(i.decode(from_encode).encode(to_encode)) else: file_pteh = os.path.join(os.getcwd(), root) convert(file_pteh, target_filePath, from_encode, to_encode) else: print 'fuck!'
root = '/home/haining/bbs' tar = '/home/haining/tmp' convert(root, tar)
|