Python Forum
How to pass arguments to popen?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to pass arguments to popen?
#1
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"])
Reply
#2
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'
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#4
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to pass encrypted pass to pyodbc script tester_V 0 853 Jul-27-2023, 12:40 AM
Last Post: tester_V
  Possible to dynamically pass arguments to a function? grimm1111 2 2,171 Feb-21-2021, 05:57 AM
Last Post: deanhystad
  Why Pass Functions as arguments? muzikman 14 5,647 Jan-18-2021, 12:08 PM
Last Post: Serafim
  how to pass arguments between pythons scripts? electricDesire 2 2,157 Oct-19-2020, 07:19 PM
Last Post: electricDesire
  how to pass the interactive string to Popen subprocess maiya 1 1,879 Sep-18-2020, 09:36 PM
Last Post: Larz60+
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,553 Sep-07-2020, 08:02 AM
Last Post: perfringo
  How to pass multiple arguments into function Mekala 4 2,447 Jul-11-2020, 07:03 AM
Last Post: Mekala
  Pass Arguments to Function phillyfa 2 2,022 Mar-27-2020, 12:05 PM
Last Post: phillyfa
  Pass by reference vs Pass by value leodavinci1990 1 2,199 Nov-20-2019, 02:05 AM
Last Post: jefsummers
  # of Positional arguments to pass for creating an object? burvil 2 3,829 Sep-09-2017, 06:43 PM
Last Post: burvil

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020