Python Forum
Help copying files with shutil
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help copying files with shutil
#1
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?

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())
Reply
#2
https://docs.python.org/3/library/shutil...eFileError Wrote:exception shutil.SameFileError
This exception is raised if source and destination in copyfile() are the same file.
Raised by copyfile not copy

https://docs.python.org/3/reference/simp...-statement Wrote:pass is a null operation — when it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed.
Reply
#3
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:

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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  use of shutil.copytree with ENOTDIR exception yan 2 839 Nov-29-2023, 03:02 PM
Last Post: yan
  shutil.move make data corrupt kucingkembar 0 742 Feb-01-2023, 01:30 PM
Last Post: kucingkembar
  Copying files if the name is in range tester_V 9 1,471 Nov-24-2022, 12:21 AM
Last Post: tester_V
  Merge htm files with shutil library (TypeError: 'module' object is not callable) Melcu54 5 1,492 Aug-28-2022, 07:11 AM
Last Post: Melcu54
  Copying files from a remote Windows station tester_V 11 6,748 Jul-17-2021, 02:19 AM
Last Post: tester_V
  extra slashes in the network path for shutil.copy tester_V 3 3,691 Jun-02-2021, 07:57 AM
Last Post: supuflounder
  concatenat - shutil jmabrito 3 2,142 Feb-11-2021, 12:17 PM
Last Post: jmabrito
  Shutil FileNotFoundError: Errno 2 Help lord_kaiser 8 10,356 Aug-10-2020, 08:45 AM
Last Post: lord_kaiser
  shutil.copy questions kristianpython 3 2,251 Jul-14-2020, 09:19 AM
Last Post: Gribouillis
  Shutil move if file exists in destination Friend 2 6,641 Feb-02-2020, 01:45 PM
Last Post: Friend

Forum Jump:

User Panel Messages

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