Python Forum

Full Version: Pass command line argument with quotes
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm relatively new to Python and have googled around enough to understand in general how to use os.system and subprocess, but struggling to essentially send a command like this via my python script:

C:\Program Files\Editor\Editor.exe C:\Downloads\123.pdf

When I type that into my cmd, it opens 123.pdf with Editor.

Thanks in advance!
koticphreak Wrote:but struggling to essentially send a command like this
We don't see much of the struggle. What have you tried with python code?
(May-31-2019, 10:34 PM)Gribouillis Wrote: [ -> ]What have you tried with python code?
subprocess.call(["C:\Program Files\Editor\Editor.exe", "C:\Downloads\123.pdf"])
Backslashes in strings indicate special characters, so your string does not contain the characters you think it does. There are a few ways to solve this:

subprocess.call([r"C:\Program Files\Editor\Editor.exe", "C:\Downloads\123.pdf"])      # raw string
subprocess.call(["C:\\Program Files\\Editor\\Editor.exe", "C:\\Downloads\\123.pdf"])  # escape the backslashes
subprocess.call(["C:/Program Files/Editor/Editor.exe", "C:/Downloads/123.pdf"])       # forward slashes
Forward slashes will work because they work in file paths regardless of the OS.