recursion - Python recursive file renaming -


i pretty new python , attempting create python script able recursively rename every file in directory including subdirectories. every time run script i'm getting error

oserror: [errno 2] no such file or directory

the directory contains text files , folder other files.

does know why keeps happening?

code:

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

i'm issue stems joining new paths "path" variable rather current directory returned walk.

import os path = "example path here" new_filename= "" # isn't c, don't need pre-declare variable. = 0  filenames = os.listdir(path) # line needed? # not can see, no dir,subdir,listfilename in os.walk(path):     filename in listfilename:         += 1         new_filename = 'filename' + str(i)         src = os.path.join(dir, filename) # note change here         dst = os.path.join(dir, new_filename) # , here         os.rename(src, dst) 

you should go read docs os.walk, i'm not sure understand does?

also, don't call variable dir, it'll mask builtin same name.

also also, don't need pre-declare path outside of loops.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -