Search code examples
pythonsubprocessvalueerrorshlex

Formatting String for subprocess.run "No closing quotation"


I have this command to run:

'xcopy /e "%s" "%s"'%(executablesPath + "\\*", sourcePath+"\\Executables\\")

which is formatted to:

xcopy /e "I:\Storage$\tools\Executables\*" "C:\Win10x64-1903\Executables\"

and ran within:

subprocess.run(shlex.split(command))

What is causing the error ValueError: No closing quotation?


Solution

  • shlex.split doesn't like the last backslash before the double quote. It seems like you want to escape this quote, hence the message.

    quick fix: replace sourcePath+"\\Executables\\" by os.path.join(sourcePath,"Executables")

    better fix: don't compose a command line just to split it again. Just use a list of arguments

    subprocess.run(["xcopy","/e",os.path.join(executablesPath,"*"),os.path.join(sourcePath,"Executables")])
    

    Better yet, use shutil.copytree to recursively copy a directory. No need for a non-portable external command. Something (untested) like:

    import shutil
    shutil.copytree(executablesPath,os.path.join(sourcePath,"Executables"))