0

I have a very specific command that I want to run during my python script:

"C:\Program Files\Some Folder\application.exe" /config:"C:\Program Files\Some Folder\my_config.ini"

Including the quotation marks.

my_config.ini is a a configuration file used to specify parameters for the application on startup.

The command works perfectly when I type it into cmd, but I cant get it work in vscode python ...

import os
# i have tried these
os.system('"C:\Program Files\Some Folder\application.exe" /config:"C:\Program Files\Some Folder\my_config.ini"')
os.system('"C:\Program Files\Some Folder\application.exe"' + ' /config:' + '"C:\Program Files\Some Folder\my_config.ini"')
os.system("C:\Program Files\Some Folder\application.exe /config:C:\Program Files\Some Folder\my_config.ini")

They all give this error:

'C:\Program' is not recognized as an internal or external command, operable program or batch file.

EDIT:

Given this question and answer: os.system to invoke an exe which lies in a dir whose name contains whitespace

Im trying to use this code:

file = 'C:\\Program Files\\Some Folder\\application.exe'
 subprocess.call([file])

and this works! but I still cant figure out how to add the config file to it...

  • I just noticed that, changed it to python my bad – Imightbehigh Feb 28 '22 at 03:12
  • The documentation states "The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function." - is there a reason you're trying to get this to work? It won't be that hard, but I fail to see why you'd even try? – Grismar Feb 28 '22 at 03:14
  • The backslash is an escape character for python literals. For instance `\n` is the newline character. Try turning all of your backslashes into doubles, `'"C:\\Program Files\\"'` and etc... – tdelaney Feb 28 '22 at 03:15
  • @Grismar but won't the problem be the same? – tdelaney Feb 28 '22 at 03:19
  • @Grismar the documentation for [`os.system`](https://docs.python.org/3/library/os.html#os.system) is a lot harder to find than it should be. – Mark Ransom Feb 28 '22 at 03:20
  • @MarkRansom I could certainly have added that link, so thanks for that, but unless you were being sarcastic and I missed it, I don't find that googling "python os.system documentation" qualifies as "hard to find", because that's the top result. Same for duckduckgo – Grismar Feb 28 '22 at 03:23
  • Using the subprocess.call method still produces an error. The problem isnt the white space in the folder name, its the config file specification. – Imightbehigh Feb 28 '22 at 03:23
  • The error message implies that those double quotes are not being processed by the shell correctly. I don't know why, the documentation states that for Windows it should be executing `cmd.exe` and the first example looks good to me, except for the backslash problem mentioned by @tdelaney. – Mark Ransom Feb 28 '22 at 03:23
  • @Imightbehigh your error message in the question makes it pretty clear that the lack of escape characters (or r-string) was the problem. The fact that you *also* have a configuration issue is not that surprising though. – Grismar Feb 28 '22 at 03:24
  • @Grismar no I wasn't being sarcastic. I Googled `os.system` and found the correct page easily enough, but finding the actual function was like finding a needle in a haystack. – Mark Ransom Feb 28 '22 at 03:25
  • @MarkRansom I see what you mean, but "os" and "system" are among the most generic IT phrases I can think of, so adding "Python" seems like a minimum, adding "documentation" helps if you don't want the top results to be more SO stuff, or tutorials etc. with better SEO and ad budgets. – Grismar Feb 28 '22 at 03:27
  • @Grismar i used subprocess in my edit – Imightbehigh Feb 28 '22 at 03:30
  • @Imightbehigh I have to assume you're just trolling now - the [documentation is pretty clear on how to pass parameters](https://docs.python.org/3/library/subprocess.html#subprocess.run) to `.call()`, although it's also pretty clear about `.run()` being the preferable option in most cases. In either case, just `[command, option, option, option]` works just fine. – Grismar Feb 28 '22 at 03:57

0 Answers0