I have a folder with the following contents in it:
- one folder named:
1_blocks - list of 19 csv files by years:
London_255_1999.csv,London_255_2000.csv, …,London_255_2017.csv - one other csv file:
London_xyz_combined_output_all_years.csv
The task is to rename only the 19 csv files using a for loop starting with London_255_1999.csv, …, London_255_2017.csv into London_245_1999.csv, …, London_245_2017.csv (i.e. replacing 255 to 245 in each given file name).
Here is my code. I don't want other files and folders to get renamed. Only the 19 files mentioned above.
path = r'A:\Engineering'
for f in os.listdir(path):
if f.startswith("London_255") and not f.endswith('years.csv'):
f_name, f_ext = os.path.splitext(f)
f_site, f_strings, f_year = f_name.split('_')
f_strings='245'
f_site=f_site
f_year=f_year
new_name = '{}_{}_{}{}'.format(f_site, f_strings, f_year, f_ext)
os.rename(f,new_name)
Please suggest the easiest way to rename, if any. I am getting the following error:
f_site, f_strings, f_year = f_name.split('_')
ValueError: not enough values to unpack (expected 3, got 2)