Python Forum

Full Version: How to pass arguments to popen?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to pass an argument to the popen function below. I've tried various permutations of syntax, and still can't get it working. What I ultimately want to do is pass an argument to the script, i.e., sys.argv. Can someone show me the syntax for this?

-Thanks


import subprocess

myfile = "C:\Users\joe\Pictures\Spirit Animals.jpg"

subprocess.Popen(['C:\Program Files (x86)\XYplorer\XYplorer.exe', "myfile"])
subprocess.Popen(['C:\Program Files (x86)\XYplorer\XYplorer.exe', myfile])
this way you pass the myfile variable, in your code (with the quotes) you pass the string 'myfile'
You need either to escape backslashes or prepend the path string with r prefix to force its interpretation as a raw string
r'C:\Program Files (x86)\XYplorer\XYplorer.exe'

backslash is an escape character in Python string, so the way you wrote it each character after backslash is interpreted differently.

PS same goes for myfile (with heading @buran's advice re uncommenting).

PPS Posting exception message next time may be a good idea
(Jul-08-2018, 05:10 AM)buran Wrote: [ -> ]
subprocess.Popen(['C:\Program Files (x86)\XYplorer\XYplorer.exe', myfile])
this way you pass the myfile variable, in your code (with the quotes) you pass the string 'myfile'

Thanks.