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
Post a Comment