python批量转换文件编码

因为下载了一份JAVA程序源码,在Ubuntu上全是乱码,我想也是用了GBK编码,所以打算用Python处理下。代码写的乱

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
# coding = utf-8
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)

可以实现将一个目录下的所有文件,转换成指定编码,并且保证目录结构.