Python Bulk Modify Package name

Source: Internet
Author: User

#Coding=utf-8ImportOS#Import re#set the encoding to utf-8 otherwise you will get an error. At this time sublime console will be reported garbled. But don't worry, utf-8 file doesn't get an error.Importsysreload (SYS) sys.setdefaultencoding ("Utf-8" )#Base Package NameBasepackage='com.emark.base. Annotations'#Base path name based on the base package name, here must be Unicode open .... I don't know why other strings don't have to be written like this .... , others do not--#or#basedir = Unicode ("* * * *", ' utf-8 ')#decode encoding to Utf-8 encode utf-8 to other encodings#or Basedir = u "* * *" is also UTF-8 encodedBasedir=u"e:/emark/emark-manager/src/main/java/com/emark/base/Annotations"PrintBasedir forDirpath,dirnames,filenamesinchOs.walk (basedir): forFileNameinchFilenames:start=Len (basedir) End=Len (Dirpath)#replace \ To.Despackage=dirpath[start:].replace ("\\",".") Despackage=basepackage+DespackagePrintdespackage File=os.path.join (dirpath,filename) lines=[] with open (file,'r+') as Files:lines=files.readlines ()#return listWith open (file,"w+") as files: forLineinchlines:Print LineifLine.startswith (" Package"): Line=' Package'+despackage+"\ n"Files.write (line)

The role of Decode is to convert other encoded strings into Unicode encodings, such as Str1.decode (' gb2312 '), to convert gb2312 encoded string str1 into Unicode encoding.

The role of encode is to convert Unicode encoding into other encoded strings, such as Str2.encode (' gb2312 '), to convert Unicode encoded string str2 to gb2312 encoding.

Python Bulk Modify Package name

Related Article

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: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.