Python:重命名文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python:重命名文件相关的知识,希望对你有一定的参考价值。
# simply import os os.rename('a.txt','b.kml') # or import shutil shutil.move('a.txt', 'b.kml') # or if you want to copy.. import shutil shutil.copy('a.txt', 'b.kml') # # Another example: I am trying to rename a list of files: # # File1.File1 # File2.File2 # # etc # # I need them to be renamed to: # # File1 # File2 # etc import os,shutil def renamer(target) : os.chdir(target) for filename in os.listdir('.') : print filename newfilename = os.path.splitext(filename)[0] print newfilename os.rename(filename, newfilename) print "Renamed " + filename + " to " + new_filename # test the function/module if __name__ == __main__: renamer(sys.argv[1]) # or more simply:
以上是关于Python:重命名文件的主要内容,如果未能解决你的问题,请参考以下文章