473,542 Members | 2,108 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

something similar to shutil.copytree that can overwrite?

I need to copy directories from one place to another, but it needs to
overwrite individual files and directories rather than just exiting if
a destination file already exists. Previous suggestions have focused
on looking at the source for copytree, but it has several places where
exceptions can be raised, and the documentation for the shutil
functions that copytree is implemented in terms of isn't exactly clear
about which exceptions get raised and when. This makes duplicating a
one-line shell operation a non-trivial task.

Has anybody got any pre-written code that does what I'm looking for?

--
Ben Sizer

Jun 20 '07 #1
5 18899
On Jun 20, 5:30 pm, Ben Sizer <kylo...@gmail. comwrote:
I need to copy directories from one place to another, but it needs to
overwrite individual files and directories rather than just exiting if
a destination file already exists.
What version of Python do you have?
Nothing in the source would make it exit if a target file exists.
(Unless perhaps you have sym-links or the like.)

Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)]

copytree calls copy2 which calls copyfile

from shutil.py:
#--------------------------------------------------------------------
def _samefile(src, dst):
# Macintosh, Unix.
if hasattr(os.path ,'samefile'):
try:
return os.path.samefil e(src, dst)
except OSError:
return False

# All other platforms: check for same pathname.
return (os.path.normca se(os.path.absp ath(src)) ==
os.path.normcas e(os.path.abspa th(dst)))

def copyfile(src, dst):
"""Copy data from src to dst"""
if _samefile(src, dst):
raise Error, "`%s` and `%s` are the same file" % (src, dst)

fsrc = None
fdst = None
try:
fsrc = open(src, 'rb')
fdst = open(dst, 'wb')
copyfileobj(fsr c, fdst)
finally:
if fdst:
fdst.close()
if fsrc:
fsrc.close()
#--------------------------------------------------------------------
Jun 20 '07 #2
On 20 Jun, 11:40, Justin Ezequiel <justin.mailing li...@gmail.com >
wrote:
On Jun 20, 5:30 pm, Ben Sizer <kylo...@gmail. comwrote:
I need to copy directories from one place to another, but it needs to
overwrite individual files and directories rather than just exiting if
a destination file already exists.

What version of Python do you have?
Nothing in the source would make it exit if a target file exists.
(Unless perhaps you have sym-links or the like.)
I have 2.5, and I believe the behaviour I saw was that it exits if a
directory already exists and it skips any files that already exist. It
certainly wouldn't overwrite anything.

--
Ben Sizer
Jun 21 '07 #3
def copytree(src, dst, symlinks=False) :
"""Recursiv ely copy a directory tree using copy2().

The destination directory must not already exist.

XXX Consider this example code rather than the ultimate tool.

"""
names = os.listdir(src)
if not os.path.exists( dst): os.makedirs(dst ) # add check here

Jun 22 '07 #4
On 22 Jun, 02:34, Justin Ezequiel <justin.mailing li...@gmail.com >
wrote:
def copytree(src, dst, symlinks=False) :
"""Recursiv ely copy a directory tree using copy2().

The destination directory must not already exist.

XXX Consider this example code rather than the ultimate tool.

"""
names = os.listdir(src)
if not os.path.exists( dst): os.makedirs(dst ) # add check here
That's the easy bit to fix; what about overwriting existing files
instead of copying them? Do I have to explicitly check for them and
delete them? It seems like there are several different copy functions
in the module and it's not clear what each of them do. What's the
difference between copy, copyfile, and copy2? Why do the docs imply
that they overwrite existing files when copytree skips existing
files?

--
Ben Sizer

Jun 22 '07 #5
On Jun 22, 9:07 pm, Ben Sizer <kylo...@gmail. comwrote:
That's the easy bit to fix; what about overwriting existing files
instead of copying them? Do I have to explicitly check for them and
delete them? It seems like there are several different copy functions
in the module and it's not clear what each of them do. What's the
difference between copy, copyfile, and copy2? Why do the docs imply
that they overwrite existing files when copytree skips existing
files?

--
Ben Sizer
copytree does not skip existing files
if will overwrite said files
I just missed the makedirs call at the start
the failed makedirs call will cause the copytree function to exit
completely
thus you do not get your files updated
but with the if exists check, your files should get overwritten
Jun 25 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1298
by: Eric Dubuis | last post by:
Hi there, I really do like the package documentation possibility through the package.html file with javadoc. Is there a similar possibility in .NET/C#. Giving the structure of the generated XML and the way the resulting HTML files are formatted, I must believe that there is such thing. Unfortunately I have not found anything in Books,...
7
1765
by: Tinus | last post by:
Hello all, I was wondering... Is there a way to create a Array so that I can call a value with a name i.s.o. a index number? Something similar like a DataTable. This is what I want: Declare a array of some sorts, private string test; .... I don't want to get the value by saying: test but test Can this only be done with a DataTable? But...
5
5597
by: Andreas Müller | last post by:
Hi, I was wondering, if there is something similar in VB.NET like the using statement in C#. What it does is to automatically call Dispose on the object decrared with in the statement when the block exits. using (MyBoj) { }// MyObj.Dispose is called
0
1193
by: jnair | last post by:
from the perl man pages of LWP::Simple mirror($url, $file) Get and store a document identified by a URL, using If-modified- since, and checking the Content-Length. Returns the HTTP response code. is there something similar in python
59
5066
by: MotoK | last post by:
Hi Experts, I've just joined this group and want to know something: Is there something similar to smart pointers in C or something to prevent memory leakages in C programs. Regards MotoK
3
1555
by: chrismcb | last post by:
In C++ I want to statically create an array that contains variable arrays inside of it. Or something similar. Essentially I want to do something like this: { {"name1", {a, b, c} }, {"name2", {b, c} }, {"name2", {d} } } I want an array of names, and with each name I want to associate an list of
10
1337
by: RickMuller | last post by:
One of my all-time favorite scripts is parseline, which is printed below def parseline(line,format): xlat = {'x':None,'s':str,'f':float,'d':int,'i':int} result = words = line.split() for i in range(len(format)): f = format trans = xlat.get(f,'None')
4
2596
by: klia | last post by:
hello folks i am trying to tweak the current codes so that later when i call it from the terminal i can provide sourcefile and the destination file rather being fixed in the code. because now i have to specify the sourcefile and the destinationfile in codes and not left to be specified from the terminal. i want to be able to do this. ...
0
7330
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7721
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7670
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5867
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5246
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1798
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
943
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
620
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.