How do you copy files from one location to another?

Tim Golden mail at timgolden.me.uk
Fri Jun 17 04:04:34 EDT 2011


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



More information about the Python-list mailing list