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


Messages In This Thread
Help copying files with shutil - by hikerguy62 - Aug-02-2019, 07:38 PM
RE: Help copying files with shutil - by Yoriz - Aug-02-2019, 07:58 PM
RE: Help copying files with shutil - by hikerguy62 - Aug-02-2019, 08:16 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  use of shutil.copytree with ENOTDIR exception yan 2 919 Nov-29-2023, 03:02 PM
Last Post: yan
  shutil.move make data corrupt kucingkembar 0 802 Feb-01-2023, 01:30 PM
Last Post: kucingkembar
  Copying files if the name is in range tester_V 9 1,553 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,606 Aug-28-2022, 07:11 AM
Last Post: Melcu54
  Copying files from a remote Windows station tester_V 11 6,904 Jul-17-2021, 02:19 AM
Last Post: tester_V
  extra slashes in the network path for shutil.copy tester_V 3 3,784 Jun-02-2021, 07:57 AM
Last Post: supuflounder
  concatenat - shutil jmabrito 3 2,208 Feb-11-2021, 12:17 PM
Last Post: jmabrito
  Shutil FileNotFoundError: Errno 2 Help lord_kaiser 8 10,558 Aug-10-2020, 08:45 AM
Last Post: lord_kaiser
  shutil.copy questions kristianpython 3 2,328 Jul-14-2020, 09:19 AM
Last Post: Gribouillis
  Shutil move if file exists in destination Friend 2 6,782 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