1

I'm new to this python and trying to write a program to recursively copy jpgs in a folder structure to a new folder, and to change the filename if their is a duplicate filename.

srcDir = 'Users/photos/2008/thumbnails/' #files in two more dir deep 
toDir = 'Users/external_drive/2008/'

shutil.copy(srcDir,toDir)
if filename = filename
    filename + '_2.jpg'
nawgoo
  • 13
  • 3

2 Answers2

1

The following script should do what you need. It uses os.walk to recurse through all of your folders looking for files with a .jpg extension. If a filename already exists in the toDir, it keeps incrementing a file counter until an empty slot is found and it displays a log of all the copies as it goes:

import os, shutil

srcDir = 'Users/photos/2008/thumbnails/' #files in two more dir deep 
toDir = 'Users/external_drive/2008/'

try:
    os.makedirs(toDir)
except:
    pass

for root, dirs, files in os.walk(srcDir, topdown=True):
    for file in files:
        src = os.path.join(root, file)
        target = os.path.join(toDir, file)

        if file.lower().endswith('.jpg'):
            index = 1

            while os.path.exists(target):
                index +=1
                target = os.path.join(toDir, os.path.splitext(file)[0]+ "_%d.jpg" % index)

            print "Copying: '%s'  to  '%s'" % (src, target)
            shutil.copy(src, target)
Martin Evans
  • 45,791
  • 17
  • 81
  • 97
0

Please note that there are some errors in your code:

  • = is an assignment in python; == checks for equality
  • shutil.copy() expects a file, not a directory, as source

For traversing a directory tree in python you can take a look at this nice tutorial.

For checking if a file exists have a look at this stackoverflow question.

b3000
  • 1,547
  • 1
  • 15
  • 27