I have hundreds of files in subdirectories that need to be renamed and then moved to a new location specified in a csv column (the path depends on the ID and reference ID). I need to rename the files in their original location first since some may have the same filename (e.g. new_reqest_11_02_01.pdf).
Current structure + filename:
A/new_reqest_11_02_01.pdf
B/new_reqest_11_02_01.pdf
B/new_reqest_11_03_01.pdf
Desired Structure + filename:
A/123456/A_F_11_02_01.pdf
B/123964/B_R_11_02_01.pdf
B/158964/B_G_11_02_01.pdf
The filename and path will be created using columns in the csv:
Client | RequestID | OldFileName | Type | Date |
---|---|---|---|---|
A | 123456 | new_reqest_11_02_01.pdf | F | 11_02_01 |
B | 123964 | new_reqest_11_03_01.pdf | R | 11_02_01 |
I can move all files and rename them, but that doesn't work because files with the same name are overwritten. I've tried creating the directory tree (successfully) from another question, but the reference to csv columns to rename the file and create a new path stump me.
import os
import csv
with open('C:\\Users\\me\\Desktop\\Testing\\Rename_Refile.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter = ',')
header = next(readCSV)
for row in readCSV:
dirname = "/".join((row[0], row[1], row[2]))
if not os.path.exists(dirname):
os.makedirs(dirname)
I tried to mishmash misc. code across stackoverflow, but it's too disjointed and I haven't been able to find a question on changing a file path using csv column's.
Any help is appreciated!