Search code examples
pythonxcopy

Xcopy with Python


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


Solution

  • \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.