I'm messing around with the shutil module (brand new to Python) and trying
to figure out how to copy a file without overwriting it. The below works,
BUT, if I run it multiple times, it overwrites the file (even thought the
except statement is in there).
Also, in the sample I found, they included the word "pass" in the exception. I have it commented out, but what is it for?
to figure out how to copy a file without overwriting it. The below works,
BUT, if I run it multiple times, it overwrites the file (even thought the
except statement is in there).
Also, in the sample I found, they included the word "pass" in the exception. I have it commented out, but what is it for?
1 2 3 4 5 6 7 8 9 10 11 12 |
source = "C:\\Users\\tom\\Desktop\\testfile1.txt" target = "C:\\tom\\TESTFOLDER1\\" try : shutil.copy(source, target) except shutil.SameFileError as e: print ( "File already exists." ) # pass except IOError as e: print ( "Unable to copy file. %s" % e) except : print ( "Unexpected error:" , sys.exc_info()) |