Batch File Rename with Python

前提是你 提交于 2021-02-05 12:15:25

问题


Below is my code to batch rename pictures inside a given directory

def multi_filename_change():
i = 0
files = askstring('Select your folder', 'Paste your directory path where your images are stored.')
for file in os.listdir(files):
    if not file.startswith('.'):
        file_name = askstring('Add file name', 'Please enter a name for your files.')
        src = file
        dst = file_name + str(i) + ".jpg"
        os.rename(src, dst)
        i += 1

When this is run I get the below error message:

os.rename(src, dst) FileNotFoundError: [Errno 2] No such file or directory: '360007_space-wallpaper-4k.jpg' -> 'test0.jpg'

I cannot seem to solve this and its probably an easy one for you experts :)

Thanks


回答1:


Source should be appended with existing directory, not just filename

src =files+file

Or
src=os.path.join(files, file)



来源:https://stackoverflow.com/questions/60577584/batch-file-rename-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!