#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