We're back after a server migration that caused effbot.org to fall over a bit harder than expected. Expect some glitches.

How do I copy a file on Windows?

Tim Golden writes:

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.

  • If your needs are simple, and you’re not bothered about portability, then the os.system approach might well cut the mustard. This will simply call the underlying OS command line and will do whatever that does.
  • If you’re after portability and/or prefer to use Python code where possible, then the shutil.copy function is probably the best bet. It uses Python-only code.
  • If you’re keen to use the Win32 API where possible, or are concerned not to clobber existing files, then the CopyFile function from the win32file module of the pywin32 extensions may be what you’re after.
  • Finally, the Platinum Option is probably to use the shell functionality which will offer the same facilities as Explorer, including animated icons, the recycle bin and renaming on collision.

For details, see http://tgolden.sc.sabren.com/python/win32_how_do_i/copy-a-file.html

CATEGORY: windows