Aug-02-2019, 08:16 PM
(This post was last modified: Aug-02-2019, 08:16 PM by hikerguy62.)
I found a workaround using the os module, but I'd still like to know how to check for and handle cases we're I'm copying a file to a directory where that file already exists. And which is better to handle file copying, deleting, etc? The os module or the shutil module?
Here's how I'm doing it using the os module:
Here's how I'm doing it using the os module:
import os import shutil source = "C:\\Users\\TIM\\Desktop\\testfile1.txt" target = "C:\\TIM\\TESTFOLDER1\\testfile1.txt" fileexists = os.path.exists("C:\\TIM\\TESTFOLDER1\\testfile1.txt") if fileexists: print("File not copied. File already exists in destination folder.") exit else: shutil.copy(source, target)