Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #7865
| Date | 2011-06-17 15:15 -0700 |
|---|---|
| From | Ethan Furman <ethan@stoneleaf.us> |
| Subject | Re: How do you copy files from one location to another? |
| References | <94a80c85-7a66-4de5-ae35-d4a4b0ea7e37@v8g2000yqb.googlegroups.com> <960dr2Fi6oU1@mid.individual.net> <7b3bf7e0-9691-4127-b332-f2224c70c4b1@y7g2000prk.googlegroups.com> <mailman.89.1308338596.1164.python-list@python.org> <4f718344-f57c-4213-a175-658e5f939775@l6g2000vbn.googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.95.1308348061.1164.python-list@python.org> (permalink) |
John Salerno wrote:
> On Jun 17, 2:23 pm, Terry Reedy <tjre...@udel.edu> wrote:
>
>> If you follow the second part of Greg's suggestion 'or one of the other
>> related function in the shutil module', you will find copytree()
>> "Recursively copy an entire directory tree rooted at src. "
>
> Yeah, but shutil.copytree says:
>
> "The destination directory, named by dst, must not already exist"
>
> which again brings me back to the original problem. All I'm looking
> for is a simple way to copy files from one location to another,
> overwriting as necessary, but there doesn't seem to be a single
> function that does just that.
If you don't mind deleting what's already there:
shutil.rmtree(...)
shutil.copytree(...)
If you do mind, roll your own (or borrow ;):
8<-------------------------------------------------------------------
#stripped down and modified version from 2.7 shutil (not tested)
def copytree(src, dst):
names = os.listdir(src)
if not os.path.exists(dst): # no error if already exists
os.makedirs(dst)
errors = []
for name in names:
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
try:
if os.path.isdir(srcname):
copytree(srcname, dstname, symlinks, ignore)
else:
copy2(srcname, dstname)
except (IOError, os.error), why:
errors.append((srcname, dstname, str(why)))
# catch the Error from the recursive copytree so that we can
# continue with other files
except Error, err:
errors.extend(err.args[0])
if errors:
raise Error(errors)
8<-------------------------------------------------------------------
~Ethan~
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How do you copy files from one location to another? John Salerno <johnjsal@gmail.com> - 2011-06-16 22:06 -0700
Re: How do you copy files from one location to another? Andrew Berg <bahamutzero8825@gmail.com> - 2011-06-17 01:15 -0500
Re: How do you copy files from one location to another? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2011-06-17 19:25 +1200
Re: How do you copy files from one location to another? John Salerno <johnjsal@gmail.com> - 2011-06-17 09:17 -0700
Re: How do you copy files from one location to another? Terry Reedy <tjreedy@udel.edu> - 2011-06-17 15:23 -0400
Re: How do you copy files from one location to another? John Salerno <johnjsal@gmail.com> - 2011-06-17 14:27 -0700
Re: How do you copy files from one location to another? Ethan Furman <ethan@stoneleaf.us> - 2011-06-17 15:15 -0700
Re: How do you copy files from one location to another? John Salerno <johnjsal@gmail.com> - 2011-06-17 16:28 -0700
Re: How do you copy files from one location to another? Tim Golden <mail@timgolden.me.uk> - 2011-06-17 09:04 +0100
Re: How do you copy files from one location to another? Heather Brown <heather@dejaviewphoto.com> - 2011-06-17 12:43 -0400
Re: How do you copy files from one location to another? Michael Hrivnak <mhrivnak@hrivnak.org> - 2011-06-18 13:13 -0400
Re: How do you copy files from one location to another? Terry Reedy <tjreedy@udel.edu> - 2011-06-18 16:52 -0400
csiph-web