Xcopy with Python
I am trying to get xcopy to work with python to copy files to a remote system. I am using a very simple test case:
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 xcopy directly from the command line works great. Any ideas?
thank
\t
is a tab character. I would suggest using raw lines for windows:
src = r"C:\<Username>\Desktop\test2.txt"
dst = r"C:\Users\<Username>"
This will stop python from being surprised by interpreting some of your backslashes as escape sequences.
In addition to using the original string literals, use subprocess
instead os.system
- it will take care of quoting your arguments correctly if they contain spaces. Thus:
import subprocess
src = r'C:\<Username>\Desktop\test2.txt'
dst = r'C:\Users\<Username>'
subprocess.call(['xcopy', src, dst])
Try to prefix your strings with r
. So r"C:\<Username>\Desktop\test2.txt"
. The problem is that the backslash is treated as a special character in strings.