19

I just need a hint on how to do things properly.

Say I have a script called script.py which uses a list of names as argument ["name1", "name2", etc. ].

I want to call this script from another script using the subprocess module. So what I would like to do is the following :

myList = ["name1", "name2", "name3"]
subprocess.Popen(["python", "script.py", myList])

Of course that doesn't work because the subprocess.Popen method requires a list of strings as arguments. So I considered doing the following :

subprocess.Popen(["python", "script.py", str(myList)])

Now the process starts but it doesn't work because it has a string as argument and not a list. How should I fix that properly?

Serge
  • 1,018
  • 2
  • 11
  • 14

2 Answers2

39

Concatenate them using + operator.

myList = ["name1", "name2", "name3"]
subprocess.Popen(["python", "script.py"] + myList)

BTW, if you want use same python program, replace "python" with sys.executable.

falsetru
  • 357,413
  • 63
  • 732
  • 636
  • or `.extend()` it if you are unsure about the type of the additional arguments. – Niklas R Oct 30 '13 at 13:38
  • @NiklasR, or `+ list(myList)` – falsetru Oct 30 '13 at 13:40
  • 1
    `extend` is only useful if you modify a named list prior to the call. `subprocess.Popen(["python", "script.py"].extend(myList))` would pass `None` (the return value of `extend`) as the first and only argument to `Popen()`. – chepner Oct 30 '13 at 13:45
  • @falsetru, can we pass a new parameter (just like we pass to a function) from previous process to the new sub-process? – user5319825 Aug 26 '16 at 07:34
  • @user5319825, Yes, as you can see in the code in the answer. The sub-process can receive those command line parameters using [`sys.argv`](https://docs.python.org/3/library/sys.html#sys.argv). – falsetru Aug 26 '16 at 11:07
  • I meant to pass an argument from the process which is executing to the process which is yet to start after a while (obviously to the next coming process) – user5319825 Aug 26 '16 at 12:39
  • @user5319825, I don't get it. Please post a separate question. – falsetru Aug 26 '16 at 12:47
4

Thanks for the quick answer falsetru. It doesn't work directly but I understand how to do. You're suggestion is equivalent to doing :

subprocess.Popen(["Python","script.py","name1","name2","name3"])

Where I have 3 arguments that are the strings contained in my original list.

All I need to do in my script.py file is to build a new list from each argument received by doing the following :

myList = sys.argv[1:]

myList is now the same than the one I had initially!

["name1","name2","name3"]

Don't know why I didn't think about it earlier!

Serge
  • 1,018
  • 2
  • 11
  • 14
  • best way that worked for me was to use the `extend` to add more elements into the list. The `extend` came in handy as i have still to decide what to add into the list before calling `subprocess.Popen()` – daparic Mar 16 '18 at 16:55
  • thanks... place myList = sys.argv[1:] after if __name__ == '__main__': – Abolfazl Rastgou Feb 19 '23 at 17:23