Created
June 7, 2018 18:48
-
-
Save mancap314/8ee899baee410e329c4a56d15e0ff393 to your computer and use it in GitHub Desktop.
rename files iteratively
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
import sys | |
import os | |
mapping = {'cz': 'czech', | |
'pl': 'poland'} | |
start_dir = sys.argv[1] | |
rootdir = Path(start_dir) | |
# For absolute paths instead of relative the current dir | |
file_list = [f for f in rootdir.resolve().glob('**/*') if f.is_file()] | |
for f in file_list: | |
dir_name = os.path.dirname(f) | |
f_name = os.path.basename(f) | |
org_name = f_name | |
for country_code, country_name in mapping.items(): | |
f_name = f_name.replace(country_code, country_name) | |
os.rename(os.path.join(dir_name, org_name), os.path.join(dir_name, f_name)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment