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


Groups > comp.lang.python > #7801 > unrolled thread

How do you copy files from one location to another?

Started byJohn Salerno <johnjsal@gmail.com>
First post2011-06-16 22:06 -0700
Last post2011-06-18 16:52 -0400
Articles 12 — 8 participants

Back to article view | Back to comp.lang.python


Contents

  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

#7801 — How do you copy files from one location to another?

FromJohn Salerno <johnjsal@gmail.com>
Date2011-06-16 22:06 -0700
SubjectHow do you copy files from one location to another?
Message-ID<94a80c85-7a66-4de5-ae35-d4a4b0ea7e37@v8g2000yqb.googlegroups.com>
Based on what I've read, it seems os.rename is the proper function to
use, but I'm a little confused about the syntax. Basically I just want
to write a simple script that will back up my saved game files when I
run it. So I want it to copy a set of files/directories from a
location on my C:\ drive to another directory on my E:\ drive. I don't
want to rename or delete the originals, just move them. I also want
them to automatically overwrite whatever already happens to be in the
location on the E:\ drive.

Is os.rename the proper function for this? Mainly I was because the
Module Index says this:

"On Windows, if dst already exists, OSError will be raised even if it
is a file.."

so it sounds like I can't move the files to a location where those
file names already exist.

[toc] | [next] | [standalone]


#7804

FromAndrew Berg <bahamutzero8825@gmail.com>
Date2011-06-17 01:15 -0500
Message-ID<mailman.62.1308291364.1164.python-list@python.org>
In reply to#7801
On 2011.06.17 12:06 AM, John Salerno wrote:
> "On Windows, if dst already exists, OSError will be raised even if it
> is a file.."
If you try to create a file or directory that already exists on Windows,
you'll get a WindowsError with error code 183:
>>> os.mkdir('C:\\common\\games')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
WindowsError: [Error 183] Cannot create a file when that file already
exists: 'C:\\common\\games'

I'm pretty sure you have to delete the existing file before you can
"overwrite" it. You can try to write the file and delete the file and
try again in an except OSError block (this will catch WindowsError as
well since it's a subclass of OSError, and it will catch similar errors
on other platforms).

[toc] | [prev] | [next] | [standalone]


#7809

FromGregory Ewing <greg.ewing@canterbury.ac.nz>
Date2011-06-17 19:25 +1200
Message-ID<960dr2Fi6oU1@mid.individual.net>
In reply to#7801
John Salerno wrote:
> I want it to copy a set of files/directories from a
> location on my C:\ drive to another directory on my E:\ drive. I don't
> want to rename or delete the originals,

It sounds like shutil.copy() is what you want, or one of the
other related functions in the shutil module.

-- 
Greg

[toc] | [prev] | [next] | [standalone]


#7831

FromJohn Salerno <johnjsal@gmail.com>
Date2011-06-17 09:17 -0700
Message-ID<7b3bf7e0-9691-4127-b332-f2224c70c4b1@y7g2000prk.googlegroups.com>
In reply to#7809
On Jun 17, 2:25 am, Gregory Ewing <greg.ew...@canterbury.ac.nz> wrote:
> John Salerno wrote:
> > I want it to copy a set of files/directories from a
> > location on my C:\ drive to another directory on my E:\ drive. I don't
> > want to rename or delete the originals,
>
> It sounds like shutil.copy() is what you want, or one of the
> other related functions in the shutil module.
>
> --
> Greg


shutil.copy(src, dst)
Copy the file src to the file or directory dst. If dst is a directory,
a file with the same basename as src is created (or overwritten) in
the directory specified. Permission bits are copied. src and dst are
path names given as strings.



This looks promising! But can src be a directory, or does it have to
be a file? For my purposes (copying a saved games folder), I don't
really need to specify particular files to copy, I just need to copy
the entire Saved Games directory, so that's what would be my src
argument if allowed.

Also, the directory I want to copy also contains a directory. Will the
contents of that directory also be copied, or do I have to do some
kind of walk-through of the directory manually?

[toc] | [prev] | [next] | [standalone]


#7849

FromTerry Reedy <tjreedy@udel.edu>
Date2011-06-17 15:23 -0400
Message-ID<mailman.89.1308338596.1164.python-list@python.org>
In reply to#7831
On 6/17/2011 12:17 PM, John Salerno wrote:
> On Jun 17, 2:25 am, Gregory Ewing<greg.ew...@canterbury.ac.nz>  wrote:

>> It sounds like shutil.copy() is what you want, or one of the
>> other related functions in the shutil module.

> This looks promising! But can src be a directory, or does it have to
> be a file? For my purposes (copying a saved games folder), I don't
> really need to specify particular files to copy, I just need to copy
> the entire Saved Games directory, so that's what would be my src
> argument if allowed.

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. "
>
> Also, the directory I want to copy also contains a directory. Will the
> contents of that directory also be copied, or do I have to do some
> kind of walk-through of the directory manually?

If you want more control of which files to copy, between 1 and all, look 
as os.walk and the glob module.

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#7862

FromJohn Salerno <johnjsal@gmail.com>
Date2011-06-17 14:27 -0700
Message-ID<4f718344-f57c-4213-a175-658e5f939775@l6g2000vbn.googlegroups.com>
In reply to#7849
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.

[toc] | [prev] | [next] | [standalone]


#7865

FromEthan Furman <ethan@stoneleaf.us>
Date2011-06-17 15:15 -0700
Message-ID<mailman.95.1308348061.1164.python-list@python.org>
In reply to#7862
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~

[toc] | [prev] | [next] | [standalone]


#7869

FromJohn Salerno <johnjsal@gmail.com>
Date2011-06-17 16:28 -0700
Message-ID<415ccd1e-c716-4783-bb91-30ba8b118695@m17g2000yqm.googlegroups.com>
In reply to#7865
On Jun 17, 5:15 pm, Ethan Furman <et...@stoneleaf.us> wrote:
> 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~

Thanks. Deleting what is already there is not a problem, I was just
hoping to have it overwritten without any extra steps, but that's no
big deal.

[toc] | [prev] | [next] | [standalone]


#7811

FromTim Golden <mail@timgolden.me.uk>
Date2011-06-17 09:04 +0100
Message-ID<mailman.67.1308297884.1164.python-list@python.org>
In reply to#7801
On 17/06/2011 06:06, John Salerno wrote:
> Based on what I've read, it seems os.rename is the proper function to
> use, but I'm a little confused about the syntax. Basically I just want
> to write a simple script that will back up my saved game files when I
> run it. So I want it to copy a set of files/directories from a
> location on my C:\ drive to another directory on my E:\ drive. I don't
> want to rename or delete the originals, just move them. I also want
> them to automatically overwrite whatever already happens to be in the
> location on the E:\ drive.
>
> Is os.rename the proper function for this? Mainly I was because the
> Module Index says this:
>
> "On Windows, if dst already exists, OSError will be raised even if it
> is a file.."
>
> so it sounds like I can't move the files to a location where those
> file names already exist.

For a Windows-only Q&D, you could use the pywin32 win32file module
which exposes the MoveFileEx[W] API:

<code>
import win32file

win32file.MoveFileExW (
   "c:/temp/blah.txt",
   "c:/temp/blah2.txt",
   win32file.MOVEFILE_REPLACE_EXISTING
)

</code>

TJG

[toc] | [prev] | [next] | [standalone]


#7832

FromHeather Brown <heather@dejaviewphoto.com>
Date2011-06-17 12:43 -0400
Message-ID<mailman.78.1308329067.1164.python-list@python.org>
In reply to#7801
On 01/-10/-28163 02:59 PM, John Salerno wrote:
> Based on what I've read, it seems os.rename is the proper function to
> use, but I'm a little confused about the syntax. Basically I just want
> to write a simple script that will back up my saved game files when I
> run it. So I want it to copy a set of files/directories from a
> location on my C:\ drive to another directory on my E:\ drive. I don't
> want to rename or delete the originals, just move them. I also want
> them to automatically overwrite whatever already happens to be in the
> location on the E:\ drive.
>
> Is os.rename the proper function for this? Mainly I was because the
> Module Index says this:
>
> "On Windows, if dst already exists, OSError will be raised even if it
> is a file.."
>
> so it sounds like I can't move the files to a location where those
> file names already exist.
>

You keep saying 'move' when you want 'copy.'  Even if os.rename would 
work across drives (it doesn't, on Windows), it still would be removing 
the original.  Similarly with move.

As Greg mentioned, you want shutil.copy(), not move nor rename.

DaveA

[toc] | [prev] | [next] | [standalone]


#7916

FromMichael Hrivnak <mhrivnak@hrivnak.org>
Date2011-06-18 13:13 -0400
Message-ID<mailman.126.1308417217.1164.python-list@python.org>
In reply to#7801
Python is great for automating sysadmin tasks, but perhaps you should
just use rsync for this.  It comes with the benefit of only copying
the changes instead of every file every time.

"rsync -a C:\source E:\destination" and you're done.

Michael

On Fri, Jun 17, 2011 at 1:06 AM, John Salerno <johnjsal@gmail.com> wrote:
> Based on what I've read, it seems os.rename is the proper function to
> use, but I'm a little confused about the syntax. Basically I just want
> to write a simple script that will back up my saved game files when I
> run it. So I want it to copy a set of files/directories from a
> location on my C:\ drive to another directory on my E:\ drive. I don't
> want to rename or delete the originals, just move them. I also want
> them to automatically overwrite whatever already happens to be in the
> location on the E:\ drive.
>
> Is os.rename the proper function for this? Mainly I was because the
> Module Index says this:
>
> "On Windows, if dst already exists, OSError will be raised even if it
> is a file.."
>
> so it sounds like I can't move the files to a location where those
> file names already exist.
> --
> http://mail.python.org/mailman/listinfo/python-list
>

[toc] | [prev] | [next] | [standalone]


#7924

FromTerry Reedy <tjreedy@udel.edu>
Date2011-06-18 16:52 -0400
Message-ID<mailman.130.1308430362.1164.python-list@python.org>
In reply to#7801
On 6/18/2011 1:13 PM, Michael Hrivnak wrote:
> Python is great for automating sysadmin tasks, but perhaps you should
> just use rsync for this.  It comes with the benefit of only copying
> the changes instead of every file every time.
>
> "rsync -a C:\source E:\destination" and you're done.

Perhaps 'synctree' would be a candidate for addition to shutil.

If copytree did not prohibit an existing directory as destination, it 
could be used for synching with an 'ignore' function.

-- 
Terry Jan Reedy

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web