I have a directory like this:
I encounter the problem that when using this function:
from os import walk
generic_name = "{project_name}"
def rename_project(src):
project_name = raw_input("Name your project: ")
for subdir, dirs, files in walk(src):
rename(subdir, subdir.replace(generic_name, project_name))
On reaching the second folder, i.e. {project_name} Planning
the whole directory has been altered. i.e. has become:
And as such it appears the for ... in walk(src):
ceases to run. Note that the loop is working correctly; I can print each directory and get the results:
for subdir, dirs, files in walk(src):
print subdir
yields...
With my limited knowledge of Python I assume that because the directory has been changed, this causes an exception to walk(src)
and means that the loop is killed.
How can I work around this to recursively loop through the directory and rename all dirs that contain {project_name}
?
Much thanks :)