4

I'm using this Python code in Windows:

shutil.move(documents_dir + "\\" + file_name, documents_dir + "\\backup\\"
            + subdir_name + "\\" + file_name)

When this code is called more times, it overwrites the destination file. I would like to move the file and if the destination already exists, to rename it

e.g. file_name = foo.pdf

and in backup folder will be foo.pdf, foo(1).pdf, foo(2).pdf etc. or similarly e.g. with dashes foo-1.pdf, foo-2.pdf etc.

martineau
  • 119,623
  • 25
  • 170
  • 301
xralf
  • 3,312
  • 45
  • 129
  • 200

1 Answers1

7

You could just check with os.path.exists() as you're going.

import os
import shutil

file_name = 'test.csv'
documents_dir = r'C:\BR\Test'
subdir_name = 'test'

# using os.path.join() makes your code easier to port to another OS
source = os.path.join(documents_dir, file_name)
dest = os.path.join(documents_dir, 'backup', subdir_name, file_name)

num = 0
# loop until we find a file that doesn't exist
while os.path.exists(dest):
    num += 1

    # use rfind to find your file extension if there is one
    period = file_name.rfind('.')
    # this ensures that it will work with files without extensions
    if period == -1:
        period = len(file_name)

    # create our new destination
    # we could extract the number and increment it
    # but this allows us to fill in the gaps if there are any
    # it has the added benefit of avoiding errors 
    # in file names like this "test(sometext).pdf"
    new_file = f'{file_name[:period]}({num}){file_name[period:]}'

    dest = os.path.join(documents_dir, 'backup', subdir_name, new_file)

shutil.move(source, dest)

Or since this is probably used in a loop you could just drop it into a function.

import os
import shutil

def get_next_file(file_name, dest_dir):
    dest = os.path.join(dest_dir, file_name)
    num = 0

    while os.path.exists(dest):
        num += 1

        period = file_name.rfind('.')
        if period == -1:
            period = len(file_name)

        new_file = f'{file_name[:period]}({num}){file_name[period:]}'

        dest = os.path.join(dest_dir, new_file)

    return dest

file_name = 'test.csv'
documents_dir = r'C:\BR\Test'
subdir_name = 'test'

source = os.path.join(documents_dir, file_name)

dest = get_next_file(file_name, os.path.join(documents_dir, 'backup', subdir_name))

shutil.move(source, dest)

Axe319
  • 4,255
  • 3
  • 15
  • 31