Taku_
Taku_

Reputation: 1625

renaming files with CSV and glob

I am looking make a script that renames all of the PDF files I have based on a .CSV file and they need to match.

the CSV file has numbers like this:

P20084579
P10092865
P10147356
P20154177
P10028030
P10058367
P10122918
P10122478
P20008810
P10029609
P20015658

the PDF files are named like:

All_C_2017.1.pdf
All_C_2017.2.pdf
All_C_2017.3.pdf
All_C_2017.4.pdf
All_C_2017.5.pdf
All_C_2017.6.pdf
All_C_2017.7.pdf
All_C_2017.8.pdf
All_C_2017.9.pdf
All_C_2017.10.pdf
All_C_2017.11.pdf

etc etc, The first number of the CSV file (P20084579) goes with All_C_2017.1.pdf and so on, so I want to rename all these that Number to say P20084579.pdf

I understand how I can read in CSV files and globs

f =  open('Test Names.csv', 'rb')
reader = csv.reader(f)
for row in reader:
    print row[0]

pdf = glob.glob('*.pdf')
for pdfname in pdf:
    print pdfname 

and I was hoping I could do something with the OS.rename() module to have a loop that would be

os.rename(pdfname,row[0])

But when I use the GLOB to list out the directories it lists like this so it will rename the file incorrectly if I try to go this method:

    All_C_2017.1.pdf
    All_C_2017.10.pdf
    All_C_2017.11.pdf
    All_C_2017.2.pdf
    All_C_2017.3.pdf
    All_C_2017.4.pdf
    All_C_2017.5.pdf
    All_C_2017.6.pdf
    All_C_2017.7.pdf
    All_C_2017.8.pdf
    All_C_2017.9.pdf

Any suggestions?

Upvotes: 3

Views: 211

Answers (1)

Jean-François Fabre
Jean-François Fabre

Reputation: 140178

just zip (interleave) both results (from glob.glob and csv.reader) and rename the files in the loop:

with open('Test Names.csv', 'r') as f:
    for pdfname,row in zip(glob.glob('*.pdf'),csv.reader(f)):
        os.rename(pdfname,row[0]+".pdf")

notes:

  • it could use smart unpacking (new_basename,) to directly extract first (and only) row from csv file but it seems to exist more columns, so forget it.
  • if the length of the .csv file is insufficient, the renaming will stop. It's better to make sure that the sizes are equal. The operation may be difficult to undo.

Safe(r) version:

with open('Test Names.csv', 'r') as f:
    pdfs = glob.glob('*.pdf')
    reader = list(csv.reader(f))
    if len(pdfs)!=len(reader):
       raise Exception("Length mismatch")

    for pdfname,row in zip(pdfs,reader):
        os.rename(pdfname,row[0]+".pdf")

Upvotes: 1

Related Questions