Xcopy with Python

DJMcCarthy12 picture DJMcCarthy12 · Aug 4, 2014 · Viewed 9k times · Source

I'm trying to get xcopy working with python to copy files to a remote system. I am using a very simple test example:

import os

src = "C:\<Username>\Desktop\test2.txt"
dst = "C:\Users\<Username>"

print os.system("xcopy %s %s" % (src, dst))

But for some reason when I run this I get:

Invalid number of parameters
4

Running the xcopy directly from the command line works fine. Any ideas?

Thanks

Answer

FatalError picture FatalError · Aug 4, 2014

\t is a tab character. I'd suggest using raw strings for windows paths:

src = r"C:\<Username>\Desktop\test2.txt"
dst = r"C:\Users\<Username>"

This will stop python from surprising you by interpreting some of your backslashes as escape sequences.