Print

Introduction

The requirement: to copy a file or directory to another location

On the surface, this looks simple. And, indeed, it can be quite simple with no need for complications. But, if complications arise, it's worth knowing what your options are. I outline four possibilities below, each with pros and cons, and these don't include some of the more esoteric possibilities such as WMI or the Windows Scripting Host, which might be suitable for you if, say, you were working with them in your code already.

Simple: use os.system

You can call any command line tool, including the venerable copy and xcopy commands using os.system (or the newer subprocess module).

import os
import tempfile

filename1 = tempfile.mktemp (".txt")
open (filename1, "w").close ()
filename2 = filename1 + ".copy"
print filename1, "=>", filename2

os.system ("copy %s %s" % (filename1, filename2))

if os.path.isfile (filename2): print "Success"

dirname1 = tempfile.mktemp (".dir")
os.mkdir (dirname1)
dirname2 = dirname1 + ".copy"
print dirname1, "=>", dirname2

os.system ("xcopy /s %s %s" % (dirname1, dirname2))

if os.path.isdir (dirname2): print "Success"

Portable: use shutil.copy

The shutil module has functions for different kinds of copying, with and without attributes, directory trees and so on.

import os
import shutil
import tempfile

filename1 = tempfile.mktemp (".txt")
open (filename1, "w").close ()
filename2 = filename1 + ".copy"
print filename1, "=>", filename2

shutil.copy (filename1, filename2)

if os.path.isfile (filename2): print "Success"

dirname1 = tempfile.mktemp (".dir")
os.mkdir (dirname1)
dirname2 = dirname1 + ".copy"
print dirname1, "=>", dirname2

shutil.copytree (dirname1, dirname2)

if os.path.isdir (dirname2): print "Success"

Win32-specific: use win32file.CopyFile

The win32file module of the pywin32 extensions has everything you could want for manipulating files under Win32 (and quite a bit more, in my experience). In particular, the CopyFile function.

import os
import win32file
import tempfile

filename1 = tempfile.mktemp (".txt")
open (filename1, "w").close ()
filename2 = filename1 + ".copy"
print filename1, "=>", filename2

#
# Do a straight copy first, then try to copy without
# failing on collision, then try to copy and fail on
# collision. The first two should succeed; the third
# should fail.
#
win32file.CopyFile (filename1, filename2, 1)
win32file.CopyFile (filename1, filename2, 0)
win32file.CopyFile (filename1, filename2, 1)

if os.path.isfile (filename2): print "Success"

dirname1 = tempfile.mktemp (".dir")
os.mkdir (dirname1)
dirname2 = dirname1 + ".copy"
print dirname1, "=>", dirname2

#
# The CopyFile functionality doesn't seem to cope
# with directories.
#
win32file.CopyFile (dirname1, dirname2, 1)

if os.path.isdir (dirname2): print "Success"

Win32 shell: use SHFileOperation

The Windows shell — essentially Explorer and everything that goes with it — has a lot of potential for Windows programmers. It is, though, a bit more involved than perhaps it might be. The pywin32 extensions give access to it via the win32com.shell package. The function of use here is the SHFileOperation, which covers a multitude of shims.

import os
from win32com.shell import shell, shellcon
import tempfile

filename1 = tempfile.mktemp (".txt")
open (filename1, "w").close ()
filename2 = filename1 + ".copy"
print filename1, "=>", filename2

#
# Do a straight copy first, then try to copy with
# rename-on-collision, then without. The first two
# should succeed (the second with a confirmation dialog);
# the third should fail.
#
shell.SHFileOperation (
  (0, shellcon.FO_COPY, filename1, filename2, 0, None, None)
)
shell.SHFileOperation (
  (0, shellcon.FO_COPY, filename1, filename2, shellcon.FOF_RENAMEONCOLLISION, None, None)
)
shell.SHFileOperation (
  (0, shellcon.FO_COPY, filename1, filename2, 0, None, None)
)

if os.path.isfile (filename2): print "Success"

dirname1 = tempfile.mktemp (".dir")
os.mkdir (dirname1)
dirname2 = dirname1 + ".copy"
print dirname1, "=>", dirname2

#
# The CopyFile functionality doesn't seem to cope
# with directories.
#
shell.SHFileOperation (
  (0, shellcon.FO_COPY, dirname1, dirname2, 0, None, None)
)

if os.path.isdir (dirname2): print "Success"