我想将文件从说{file1}重命名为{file2}。我os.rename(file1,file2)
在python中读过并且能够做到这一点。
仅当文件与python脚本放置在同一文件夹中时,我才成功,所以我想问一下如何重命名其他文件夹的文件,即与放置python脚本的文件夹不同的文件夹。
只需使用完整路径,而不是相对路径即可:
oldFile = 'C:\\folder\\subfolder\\inFile.txt'
newFile = 'C:\\foo\\bar\\somewhere\\other\\outFile.txt'
os.rename(oldFile, newFile)
要获得双斜杠行为,您可以执行以下操作
import os
oldFile = r'C:\folder\subfolder\inFile.txt' # note the r character for raw string
os.path.normpath(oldFile)
输出
'C:\\folder\\subfolder\\inFile.txt'
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句