Skip to content

Instantly share code, notes, and snippets.

@shamilnabiyev
Last active June 2, 2019 21:16
Show Gist options
  • Save shamilnabiyev/0ebcfe6710e57b023cd072ed4be9f1be to your computer and use it in GitHub Desktop.
Save shamilnabiyev/0ebcfe6710e57b023cd072ed4be9f1be to your computer and use it in GitHub Desktop.
Rename multiple subfolders and files in python
basedir = "./"
for folder in os.listdir(basedir):
new_folder_name = folder.translate(str.maketrans({" ": r"-",".": r"_"})) + "_"
os.rename(os.path.join(basedir, folder), os.path.join(basedir, new_folder_name) )
for folder in os.listdir(basedir):
inner_path = basedir + folder + "/"
for file in os.listdir(inner_path):
os.rename(os.path.join(inner_path, file), os.path.join(inner_path, folder + file) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment