-
一个批量转换文本文件编码的程序(Python)
-
- #!/usr/bin/python
- import os,sys
- def convert( filename, in_enc = "GBK", out_enc="UTF-8" ):
- # read the file
- fp = open( filename )
- content = fp.read()
- fp.close()
- # convert the concent
- try:
- new_content = content.decode( in_enc ).encode( out_enc )
- #write to file
- fp = open( filename, 'w' )
- fp.write( new_content )
- fp.close()
- except:
- print " error... "
- def explore( dir ):
- for root, dirs, files in os.walk( dir ):
- for file in files:
- path = os.path.join( root, file )
- print "convert " + path,
- convert( path )
- print " done"
- def main():
- if len( sys.argv ) > 1 :
- path = sys.argv[1]
- if os.path.isfile( path ):
- convert( path )
- elif os.path.isdir( path ):
- explore( path )
- if __name__ == "__main__":
- main()
一个批量转换文本文件编码的程序(Python)
最新推荐文章于 2023-03-20 20:58:24 发布