一个批量转换文本文件编码的程序(Python)
2014-12-17 00:40
344 查看
一个批量转换文本文件编码的程序(Python)
#!/usr/bin/pythonimport 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)
- [转]一个批量转换文本文件编码的程序(Python)
- python 试题:写一段程序逐行读入一个文本文件,并在屏幕上打印出来。
- 一个批量转换文件编码的python脚本
- python 读一个utf8编码的文本文件,为啥有\xef\xbb\xbf文件头呢?怎么处理呀
- 一个python小程序,从文本文件中读取每行的两组数字,进行排序后输出.
- 一个批量转换文件编码的python脚本
- [数据库测试]强烈推荐一个python ODBC数据源插件,可支持Oracle,Db2,Mysql,Sql-server以及各种数据库版本,附例子和测试程序
- 纠错一个Python程序引发的讨论
- Python源码分析2 - 一个简单的Python程序的执行
- 一个求平均成绩的python的程序
- 如何构建一个ERP系统(需求分析、系统架构、系统设计、系统编码、测试、交付程序及文文件)。
- [Python]如何取出一个超大文本文件的最后几行
- [数据库测试]强烈推荐一个python ODBC数据源插件,可支持Oracle,Db2,Mysql,Sql-server以及各种数据库版本,附例子和测试程序
- [数据库测试]强烈推荐一个python ODBC数据源插件,可支持Oracle,Db2,Mysql,Sql-server以及各种数据库版本,附例子和测试程序
- python cgi ajax - 使用CGIHTTPServer实现一个ajax程序
- Python源码分析2 - 一个简单的Python程序的执行
- 纠错一个Python程序引发的讨论
- 一个Python-GTK的程序,用来关机,重启
- 发一个python写的多线程 代理服务器 抓取,保存,验证程序,希望喜欢python的朋友和我一起完善它