473,473 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Create 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 18893
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.samefile(src, dst)
except OSError:
return False

# All other platforms: check for same pathname.
return (os.path.normcase(os.path.abspath(src)) ==
os.path.normcase(os.path.abspath(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(fsrc, fdst)
finally:
if fdst:
fdst.close()
if fsrc:
fsrc.close()
#--------------------------------------------------------------------
Jun 20 '07 #2
On 20 Jun, 11:40, Justin Ezequiel <justin.mailingli...@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):
"""Recursively 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.mailingli...@gmail.com>
wrote:
def copytree(src, dst, symlinks=False):
"""Recursively 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
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...
7
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...
5
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...
0
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...
59
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
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",...
10
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...
4
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.