Python Batch conversion file encoding format

Source: Internet
Author: User
Tags join

The files in the working directory need to be transcoding, and the starting encoding is GBK and needs to be converted to utf-8. A lot of files, manual conversion certainly not, use Python to write a script to achieve.

Your own way of writing, for Linux,

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 The 25 26 #!/usr/bin/python #coding =utf-8 Import sys import OS, os.path import dircache import Commands def add (x,y): return x*y D EF Trans (dirname): lis = Dircache.opendir (dirname) for a in Lis:af=dirname+os.sep+a # print AF if Os.path.isdir (AF): # # Print AF trans (AF) Else: # # Print af+ "encoding=" +fi.name ft = commands.getoutput (' file-i ' +af) # # Print ft if A.find ('. HT M ') ==-1 and A.find ('. xml ') ==-1 and Ft.find (' text/')!=-1 and Ft.find (' iso-8859 ')!=-1:print ' GBK ' +ft+ ' > ' +af Commands.getoutput (' iconv-ficonv-f gbk-t utf-8-c-o ' + "+af+" "+af) Trans (OS.GETCWD ())

py2.6 the following version of the available code

?

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 26 27 Import Os,sys def convert (filename, In_enc = "GBK", out_enc= "UTF8"): Try:print "Convert" + filename, content = open ( filename). read () New_content = Content.decode (In_enc). Encode (OUT_ENC) open (filename, ' W '). Write (new_content) print Done "Except:print" error def Explore (dir): for Root, dirs, files in Os.walk (dir): for file in Files:path = Os.path. Join (root, file) convert (PATH) def main (): For Path in sys.argv[1:: If Os.path.isfile (path): Convert (path) elif Os.path . Isdir (PATH): Explore (path) if __name__ = = "__main__": Main ()

Support for py3.1 versions

?

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 26 27 28 29 30 Import OS import sys import codecs #该程序用于将目录下的文件从指定格式转换到指定格式, the default is GBK go to utf-8 def convert (file,in_enc= "GBK", out_enc= " UTF-8 "): Try:print (" Convert "+file) f=codecs.open (file, ' R ', In_enc) New_content=f.read () Codecs.open () (File, ' W ', Out_ ENC). Write (New_content) #print (F.read ()) except IOError as Err:print ("I/O error: {0}". Format (ERR)) def explore (dir) : For Root,dirs,files in Os.walk (dir): For file in Files:path=os.path.join (root,file) Convert (PATH) def main (): for Pat h in Sys.argv[1:: if (os.path.isfile (path)): Convert (path) elif Os.path.isdir (path): Explore (path) if __name__== "__main __ ": Main ()

The above mentioned is the entire content of this article, I hope you can enjoy.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.