Python WindowsError: [Error 3] The system cannot find the file specified when trying to rename
Question: 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 random
    
    directory = "C:\\whatever"
    string = ""
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    
    
    listDir = os.listdir(directory)
    
    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
    
        os.rename(path, string)
        string= ""
    


Answer: 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= ""
    

