Python Forum
Pass command line argument with quotes - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Pass command line argument with quotes (/thread-18788.html)



Pass command line argument with quotes - koticphreak - May-31-2019

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!


RE: Pass command line argument with quotes - Gribouillis - May-31-2019

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?


RE: Pass command line argument with quotes - koticphreak - May-31-2019

(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"])



RE: Pass command line argument with quotes - ichabod801 - May-31-2019

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.