The filenames.txt
contains all the names of the files that you want to change in a given directory.
It is generated by:
import os
walk = os.walk("./app/")
for dirpath, dirs, files in walk:
for filename in files:
fpath = os.path.join(dirpath, filename)
with open("filenames.txt", "a") as f:
f.write(filename + "\n")
The Test
directory is the directory whose files you are targeting.
"""Rename files.
Rename file references containing `-` with `_`
"""
import os
# Directory to traverse
walk = os.walk('./Test/')
# A file containing filenames that you want to change.
# Already generated
with open("filenames.txt") as filenames_to_change:
names = filenames_to_change.read().splitlines()
for dirpath, dirs, files in walk:
for filename in files:
fpath = os.path.join(dirpath, filename)
for name in names:
old_name = name
new_name = name.replace('-', '_')
print("{} >>> {}".format(old_name, new_name))
with open(fpath) as f:
s = f.read()
s = s.replace(old_name, new_name)
with open(fpath, "w") as f:
f.write(s)
os.rename
does it in a simple function call. \$\endgroup\$