0

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!

MzNix
  • 1
  • 2

1 Answers1

0

After some more experimenting, I have the following code modified from a few different posts (Source A, Source B)

import os
import pandas as pd
import shutil

df = pd.read_csv('C:\\Users\\me\Desktop\\SA and M letters\\Rename_Refile.csv',delimiter=',')
Old_Directory = "C:\\Users\me\\Desktop\\SA and M letters\\OldDirectory\\"
New_Directory = "C:\\Users\\me\\Desktop\\SA and M letters\\NewDirectory\\"


sub_directories = df["sub_directories"]
Request_ID = df["Request_ID"]
OriginalFileName = df["OriginalFileName"]
NewFileName = df["NewFileName"]

for row in df:
    dirname = "/".join((Old_Directory,row[1], row[4]))
    if not os.path.exists(dirname):
        os.makedirs(dirname)
            
            
for sub_dir in enumerate(sub_directories):
    old_path = os.path.join(Old_Directory, sub_dir)
    new_path = os.path.join(New_Directory, sub_directories,Request_ID)
    old_name = OriginalFileName
    new_name = NewFileName 
    try:
        os.rename(os.path.join(old_path, old_name), os.path.join(old_path, new_name))
        shutil.copy(os.path.join(old_path, new_name), os.path.join(new_path,new_name))
    except Exception as e:
        print('Rename for %s failed. Details: ' % old_name)
        print(e)

This creates the new directory tree, renames the files in their original location then copy the file to it's new path in the new directory tree created. I keep getting TypeError: join() argument must be str, bytes, or os.PathLike object, not 'tuple' though...

MzNix
  • 1
  • 2