Python Forum
"are the same file" error in shutil.copy - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: "are the same file" error in shutil.copy (/thread-4310.html)



"are the same file" error in shutil.copy - valtih - Aug-07-2017

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?


RE: "are the same file" error in shutil.copy - DeaD_EyE - Aug-07-2017

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