Python recursive file renaming
Question: I am pretty new to python and I am attempting to create a python script that
is able to recursively rename every file in a directory including
subdirectories. But every time I run the script I'm getting the error

OSError: [Errno 2] No such file or directory

The directory contains some text files and a folder with some other files.

Does anyone know why this keeps happening?

Code:

    
    
    import os
    path = "example path here"
    new_filename= ""
    i = 0
    
    filenames = os.listdir(path) # is this line needed?
    for dir,subdir,listfilename in os.walk(path):
        for filename in listfilename:
            i += 1
            new_filename = 'filename' + str(i)
            src = os.path.join(path, filename)
            dst = os.path.join(path, new_filename)
            os.rename(src, dst)
    


Answer: I'm fairly certain the issue stems from you joining new paths to the "path"
variable rather than the current directory returned from `walk`.

    
    
    import os
    path = "example path here"
    new_filename= "" # This isn't C, we don't need to pre-declare a variable.
    i = 0
    
    filenames = os.listdir(path) # is this line needed? # not that I can see, no
    for dir,subdir,listfilename in os.walk(path):
        for filename in listfilename:
            i += 1
            new_filename = 'filename' + str(i)
            src = os.path.join(dir, filename) # NOTE CHANGE HERE
            dst = os.path.join(dir, new_filename) # AND HERE
            os.rename(src, dst)
    

You should go read the docs for
[os.walk](https://docs.python.org/2/library/os.html#os.walk), I'm not sure you
fully understand what it does?

Also, don't call a variable `dir`, it'll mask the builtin by the same name.

Also also, you don't need to pre-declare `path` outside of your loops.

