Sep-21-2019, 11:56 PM
Hi all, I have a part of my code that I would like to give a little upgrade.
I'd like to change this code so it automaticly renames the file if it already exists in the destination-folder because thesame filename doesn't mean it has thesame content so I don't want to overwrite files in the destination-folder.
I've been thinking about using a timestamp but I'm not sure If and how to do it. Any tips? Ideas?
Thanks :)
import os import shutil dest = '/path/to/dest_folder' src = '/path/to/src_folder' for root, subdirs, files in os.walk(src): for file in files: path = os.path.join(root, file) shutil.move(path, dest)This code replaces files from a folder and its subfolders to one destination-folder. The problem here is: If there are 2 files with thesame filename I get an error:
Quote:shutil.Error: Destination path '/path/to/dest_folder/filename' already exists
I'd like to change this code so it automaticly renames the file if it already exists in the destination-folder because thesame filename doesn't mean it has thesame content so I don't want to overwrite files in the destination-folder.
I've been thinking about using a timestamp but I'm not sure If and how to do it. Any tips? Ideas?
Thanks :)