4

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

DJMcCarthy12
  • 3,819
  • 8
  • 28
  • 34
  • 1
    The Python documentation recommend using the [`subprocess`](https://docs.python.org/2/library/subprocess.html) module instead of `os.system`. – Roland Smith Aug 04 '14 at 18:08

3 Answers3

3

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

FatalError
  • 52,695
  • 14
  • 99
  • 116
3

In addition to using raw string literals, use the subprocess module instead of os.system - it will take care of quoting your arguments properly if they contain spaces. Thus:

import subprocess

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

subprocess.call(['xcopy', src, dst])
1

Try prefixing your strings with r. So r"C:\<Username>\Desktop\test2.txt". The problem is that a backslash is treated as a special character within strings.

Chinmay Kanchi
  • 62,729
  • 22
  • 87
  • 114