Python WindowsError: [Error 3] The system cannot find the file specified when trying to rename

前端 未结 2 696
没有蜡笔的小新
没有蜡笔的小新 2021-01-20 19:09

I can\'t figure out what\'s wrong. I\'ve used rename before without any problems, and can\'t find a solution in other similar questions.

import os
import ran         


        
2条回答
  •  轮回少年
    2021-01-20 19:21

    If you want to save back to the same directory you will need to add a path to your 'string' variable. Currently it is just creating a filename and os.rename requires a path.

    for item in listDir:
        path = os.path.join(directory, item)
    
        for x in random.sample(alphabet, random.randint(5,15)):
            string += x
    
        string += path[-4:] #adds file extension
        string = os.path.join(directory,string)
    
        os.rename(path, string)
        string= ""
    

提交回复
热议问题