Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #7865

Re: How do you copy files from one location to another?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ethan@stoneleaf.us>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; '2.7': 0.04; 'modified': 0.05; 'roll': 0.07; 'terry': 0.07; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'necessary,': 0.09; 'received:gator410.hostgator.com': 0.09; 'subject:files': 0.09; '~ethan~': 0.09; 'pm,': 0.10; 'def': 0.12; 'wrote:': 0.14; 'copytree': 0.16; 'dst,': 0.16; 'err:': 0.16; 'errors:': 0.16; 'name)': 0.16; 'overwriting': 0.16; 'received:72.11': 0.16; 'received:72.11.125': 0.16; 'received:72.11.125.166': 0.16; 'recursive': 0.16; 'reedy': 0.16; 'shutil': 0.16; 'subject:copy': 0.16; 'tested)': 0.16; 'cc:addr:python-list': 0.17; 'tree': 0.19; 'header:In-Reply-To:1': 0.21; 'cc:2**0': 0.22; 'cc:no real name:2**0': 0.23; "what's": 0.23; '(or': 0.24; "doesn't": 0.25; 'function': 0.25; 'suggestion': 0.26; "i'm": 0.27; '"the': 0.28; '(not': 0.28; 'raise': 0.28; 'subject:?': 0.29; 'exists': 0.29; 'version': 0.29; 'subject:How': 0.30; 'cc:addr:python.org': 0.30; 'second': 0.30; 'seem': 0.32; 'named': 0.32; 'does': 0.33; 'error': 0.33; 'there': 0.35; 'header:User-Agent:1': 0.35; '17,': 0.35; 'try:': 0.35; 'skip:o 20': 0.37; 'skip:e 20': 0.37; 'problem.': 0.38; 'subject:from': 0.38; 'but': 0.38; 'subject:: ': 0.38; 'skip:s 20': 0.39; 'your': 0.60; 'john': 0.62; 'back': 0.63; 'received:websitewelcome.com': 0.67; 'destination': 0.67; 'subject:one': 0.77; 'subject:you': 0.80; 'borrow': 0.84; 'received:69.41.248.84': 0.84; 'why:': 0.84; 'received:gateway02.websitewelcome.com': 0.95
Date Fri, 17 Jun 2011 15:15:41 -0700
From Ethan Furman <ethan@stoneleaf.us>
User-Agent Thunderbird 1.5.0.10 (Windows/20070221)
MIME-Version 1.0
To John Salerno <johnjsal@gmail.com>
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>
In-Reply-To <4f718344-f57c-4213-a175-658e5f939775@l6g2000vbn.googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - gator410.hostgator.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - stoneleaf.us
X-BWhitelist no
X-Source
X-Source-Args
X-Source-Dir
X-Source-Sender mail.admailinc.com ([192.168.10.136]) [72.11.125.166]:1451
X-Source-Auth ethan+stoneleaf.us
X-Email-Count 1
X-Source-Cap dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ==
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.95.1308348061.1164.python-list@python.org> (permalink)
Lines 49
NNTP-Posting-Host 82.94.164.166
X-Trace 1308348062 news.xs4all.nl 49184 [::ffff:82.94.164.166]:45609
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:7865

Show key headers only | View raw


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 | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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