我想重命名文件夹中的多个文件
当前文件名如下:
3321_AD_axial_001_MRI.jpg
3421_AD_axial_098_MRI.jpg
3521_AD_axial_032_MRI.jpg
预期的文件名如下:
images0001.jpg, images0002.jpg, images0003.jpg
filepath =“ / Users / XYZ / Desktop / SVM-Image-Classification-master / test”
import os
def main():
i = 0
for filename in os.listdir("test"):
dst ="images" + str(i) + ".jpg"
src ='test'+ filename
dst ='test'+ dst
i += 1
os.rename(scr,dst)
main()
ileNotFoundError Traceback (most recent call last)
<ipython-input-47-840511576fe7> in <module>()
18
19 # Calling main() function
---> 20 main()
<ipython-input-47-840511576fe7> in main()
11 # rename() function will
12 # rename all the files
---> 13 os.rename(src, dst)
14 i += 1
15
FileNotFoundError: [Errno 2] No such file or directory: 'test0543_AD_axial_099_PET.jpg' -> 'testimage0.jpg'
该函数生成错误,所有文件名保持不变。他们没有像我预期的那样改变。谢谢!
答案 0 :(得分:0)
这里:
import os
def main():
i = 0
for filename in os.listdir("test"):
dst ="images_" + str(i) + "_.jpg"
src = filename
path = "test/"+src
newpath = "test/"+dst
os.rename(path,newpath)
i += 1
main()