0

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.

Marc B. Hankin
  • 771
  • 3
  • 15
  • 31

1 Answers1

1

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.

Bill Horvath
  • 1,336
  • 9
  • 24
  • I tried subprocess.run(cmdlin4popup) using your string modifications, but I got the following error: FileNotFoundError: [WinError 2] The system cannot find the file specified – Marc B. Hankin Dec 14 '22 at 20:19
  • That suggests one of the files is missing. It looks like you're referencing two file locations, `wscript.popup_aaa.vbs` and `E:\Zmani\Logging\202212141045__Tmp_7602977475348411.py_.txt`. The first one should be in the same directory in which you're executing the python script. Are either of those missing on the machine you're running this on? – Bill Horvath Dec 14 '22 at 20:22
  • First, thank you for your efforts. Neither of the files is missing. I don't see why the 2 files should be or need to be in the same directory, since my original cmdlin4popup var's value runs successfully when I write it to a file "cmdlin4popup.cmd" and then invoke subprocess.run(cmdlin4popup). – Marc B. Hankin Dec 14 '22 at 21:14
  • We may be running up against the limits of my knowledge of running python in a Windows environment here. For example: to construct a path that I'll use in a subprocess, I'd usually `from pathlib import Path`, then `my_path = Path("E", "Apps", "UtilitiesByMarc", "inputBox")`, with `my_path` then passed as an argument to `subprocess`. That provides a largely platform-independent approach to path construction, meaning I don't have to worry about which OS I'm in, but it also means I don't have enough experience specifying paths directly in python on Windows to say what's happening for sure. – Bill Horvath Dec 14 '22 at 21:50