Marc B. Hankin
Marc B. Hankin

Reputation: 751

python, run win cm line many quotes for command line with arguments

The following command line (assigned to the var "cmdlin4popup") runs just fine when I paste it into a Windows cmd.exe (terminal?) window:

"E:\Apps\UtilitiesByMarc\inputBox wscript.popup_aaa.vbs" "E:\Zmani\Logging\202212141045__Tmp_7602977475348411.py_.txt" 9 "Backup Report" 65

But subprocess.Popen(cmdlin4popup) triggers an error message:

OSError: [WinError 193] %1 is not a valid Win32 application

I think that the problem is the placement of quotes, i.e., the number of double quotes and their placement, but I can't figure out what Python needs. I'd appreciate any suggestions about where to find the solution.

None of the following alternate placements of quotes ran successfully:

cmdlin4popup = "E:\Apps\UtilitiesByMarc\inputBox wscript.popup_aaa.vbs" "E:\Zmani\Logging\202212141059__Tmp_2248283756147461.py_.txt" 9 "Backup Report" 65

cmdlin4popup = """E:\Apps\UtilitiesByMarc\inputBox wscript.popup_aaa.vbs" E:\Zmani\Logging\202212141059__Tmp_2248283756147461.py_.txt 9 "Backup Report" 65"""

cmdlin4popup = "E:\Apps\UtilitiesByMarc\inputBox wscript.popup_aaa.vbs" "E:\Zmani\Logging\202212141059__Tmp_2248283756147461.py_.txt 9 "Backup Report" 65"

cmdlin4popup = "E:\Apps\UtilitiesByMarc\inputBox wscript.popup_aaa.vbs" """E:\Zmani\Logging\202212141059__Tmp_2248283756147461.py_.txt 9 "Backup Report" 65"""

I am pretty sure that I tried all of the following alternate methods to run each of the above command lines, again with no success:

subprocess.Popen(cmdlin4popup)
subprocess.run(cmdlin4popup)
subprocess.call(cmdlin4popup)
os.startfile(cmdlin4popup)

If the solution was in either of the following postings, I didn't understand the solutions: Python run command line (time), Command line passing quotes within quotes

As an interim kludge workaround (FYI for anyone else dealing with this problem), I wrote the cmdlin4popup var's value to a text file with the extension '.cmd', and successfully used subprocess.run (as recommended by Bill Horvath):

ret_val = subprocess.run(cmdlin4popup)

But it would be great to understand what the problem is.

Upvotes: 0

Views: 75

Answers (1)

Bill Horvath
Bill Horvath

Reputation: 1717

subprocess.Popen expects a list. And if you have to have the double-quotes to make it work, add single-quotes around them:

cmdlin4popup = ['"E:\Apps\UtilitiesByMarc\inputBox wscript.popup_aaa.vbs"', '"E:\Zmani\Logging\202212141045__Tmp_7602977475348411.py_.txt"', 9, '"Backup Report"', 65]

Also, consider using subprocess.run instead, which is recommended in the documentation.

Upvotes: 1

Related Questions