Can't resolve WindowsError: [Error 2] The system cannot find the file specified

前端 未结 1 698
北海茫月
北海茫月 2021-01-02 07:54

I\'m trying to rename all the pictures in a directory. I need to add a couple of pre-pending zero\'s to the filename. I\'m new to Python and I have written the following scr

相关标签:
1条回答
  • 2021-01-02 08:27

    You should pass the absolute path to os.rename. Right now your only passing the filename itself. It isn't looking in the correct place. Use os.path.join.

    Try this:

    import os
    
    path = "c:\\tmp"
    dirList = os.listdir(path)
    
    for fname in dirList:
        fileName = os.path.splitext(fname)[0]
        fileName = "00" + fname
        os.rename(os.path.join(path, fname), os.path.join(path, fileName))
        #print(fileName)
    
    0 讨论(0)
提交回复
热议问题