Python Forum

Full Version: "are the same file" error in shutil.copy
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a piece of code that used to copy a file before processing it. Now, there is no such need because file is provided right into place such that src and destination are the same now and the copy operation fails. Why is it considered an error rather than "do nothing" situation?

I guess that I can work around by copying the file into another location such that second copy operation will copy it back. Are there better workarounds?
Just use exception handling.

try:
    shutil.copy(src, dst)
except shutil.SameFileError:
    # code when Exception occur
    pass
else:
    # code if the exception does not occur
    pass
finally:
    # code which is executed always