Python Forum
"are the same file" error in shutil.copy
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"are the same file" error in shutil.copy
#1
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?
Reply
#2
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
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 249 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Copy Paste excel files based on the first letters of the file name Viento 2 423 Feb-07-2024, 12:24 PM
Last Post: Viento
  use of shutil.copytree with ENOTDIR exception yan 2 897 Nov-29-2023, 03:02 PM
Last Post: yan
  shutil.move make data corrupt kucingkembar 0 788 Feb-01-2023, 01:30 PM
Last Post: kucingkembar
  is it possible to copy image from email and place into excel file? cubangt 3 1,265 Nov-30-2022, 05:11 PM
Last Post: snippsat
  Please help me [copy and paste file from src to dst] midomarc 2 1,006 Nov-24-2022, 10:13 PM
Last Post: midomarc
  Merge htm files with shutil library (TypeError: 'module' object is not callable) Melcu54 5 1,584 Aug-28-2022, 07:11 AM
Last Post: Melcu54
  extra slashes in the network path for shutil.copy tester_V 3 3,760 Jun-02-2021, 07:57 AM
Last Post: supuflounder
  Copy column from one existing excel file to another file mkujawsk 0 5,608 Apr-14-2021, 06:33 PM
Last Post: mkujawsk
  concatenat - shutil jmabrito 3 2,179 Feb-11-2021, 12:17 PM
Last Post: jmabrito

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020