Search code examples
pythonxcopy

Windows xcopy is not working in python


When I am doing

xcopy "D:\Accessories\My File\read-me.rtf" "D:\Any Folder\Destn"

It is copying fine

Same thing I am doing in python (2.7)

import os
source = "D:\Accessories\My File\read-me.rtf"
target = "D:\Any Folder\Destn"
output = os.system ("xcopy %s %s" % (source, target))

But this code is throwing error that Invalid number of parameters

Is it a right way to invoke ? Any suggestion ?


Solution

  • There are spaces in your "source" and "target" pathnames. Try quoting it in the os.system call ie

    output = os.system ("""xcopy "%s" "%s" """ % (source, target))