Skip to content

Instantly share code, notes, and snippets.

@mancap314
Created June 7, 2018 18:48
Show Gist options
  • Save mancap314/8ee899baee410e329c4a56d15e0ff393 to your computer and use it in GitHub Desktop.
Save mancap314/8ee899baee410e329c4a56d15e0ff393 to your computer and use it in GitHub Desktop.
rename files iteratively
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