我刚接触Python,正在为一个工作项目而挣扎。你知道吗
我在目录中有文件,文件名是字符串列表的子字符串。我需要用列表中的字符串重命名文件
filenames in "./temp" = aa.txt, bb.txt, cc.txt
list_of_names = ['aa12.txt', 'bb12.txt', 'cc12.txt']
我想将文件重命名为list_of_names
中的文件。尝试了下面的代码,但出现错误
for filename in os.listdir('./temp'):
for i, item in enumerate(list_of_names):
if filename in item:
os.rename(filename,list_of_names[I])
FileNotFoundError: [Errno 2] No such file or directory: 'aa.txt' -> 'aa12.txt'
我认为这会更简单:
注意这是aa.txt文件'in'aa12.txt'将返回False。你知道吗
尝试:
另外,考虑将pathlib用于文件系统操作。你知道吗
相关问题 更多 >
编程相关推荐