| 1 | |
|---|
| 2 | import os |
|---|
| 3 | |
|---|
| 4 | def gbk2utf8(path): |
|---|
| 5 | print path |
|---|
| 6 | for root, dirs, files in os.walk(path): |
|---|
| 7 | for filename in files: |
|---|
| 8 | if filename.split('.')[-1] != 'java': continue |
|---|
| 9 | filepath= '\\'.join([root,filename]) |
|---|
| 10 | f = open(filepath, 'r') |
|---|
| 11 | utf_text = ''.join(f.readlines()) |
|---|
| 12 | f.close() |
|---|
| 13 | try: |
|---|
| 14 | unicode_text = unicode(utf_text,'GBK') |
|---|
| 15 | gb_text = unicode_text.encode('UTF-8') |
|---|
| 16 | except: |
|---|
| 17 | print 'fail:', filename |
|---|
| 18 | continue |
|---|
| 19 | f = open(filepath, 'w') |
|---|
| 20 | f.write(gb_text) |
|---|
| 21 | f.close() |
|---|
| 22 | print 'ok' |
|---|
| 23 | |
|---|
| 24 | if __name__ == '__main__': |
|---|
| 25 | gbk2utf8(r'D:\Program Files\eclipse\workspace\just') |
|---|