I have a collection of files that I would like to re-organize in a smart way, using python. The files are state-county-year specific and saved in a folder structure of this form: \state\year\county\image.jpg with many images per county. I have the following code that copies and renames all files as state_year_county_image.jpg:
import os, shutil
path = "C:\\Users\\me\\"
path_copies = "C:\\Users\\me\\copies\\"
state_list = os.listdir(path)
for state in state_list:
year_list = os.listdir(path+state)
for year in year_list:
county_list = os.listdir(path+state+"\\"+year)
for county in county_list:
image_list = os.listdir(path+state+"\\"+year+"\\"+county)
for image in image_list:
path_old = path+state+"\\"+year+"\\"+county+"\\"+image
path_new = path_copies+state+"_"+year+"_"+county+"_"+image
shutil.copy(path_old, path_new)
How can I change my file naming regime later on (e.g. from the current state_year_county_image.jpg to year_state_county_image.jpg or according to some other naming regime that I may later need)? I would be open to adopting some other basic naming convention if it allows more flexibility, but I would like the filenames to reflect the original folder names.