I’m looking for the most efficient/simple way to run a script (we’ll call it script1) with arguments that:
- call one (only one) of several other scripts (as an argument to script1), which in turn:
- has arguments on it that are given when script1 is run and are passed along to whichever script is being run based on the argument on script1…
For example, I’d like to run script1 which has arguments to call script2 which has arguments a and b:
script1.py ( script2 a b )
I’ve looked into functions and have read some people suggesting lambda but I’m fairly novice and want to learn and understand, not just be provided with an answer I don’t understand…
Just wondering what some approaches are and am looking for generic examples of how to accomplish what I want so I can program it and understand it…
Thanks in advance!!
If you want your script (one Python program, let’s call it
script1.py) to call another script (another Python program) with the given arguments, you can writescript1.pyas:sys.argvis a list that contains your program’s name (useprint sys.argv[0]and see that by yourself) and, after that, all the arguments passed to your program.sys.argv[1:]means: the contents of thesys.argvlist, starting from the second element (which has index 1) to the end of the list.subprocess.callcalls another program, and it accepts a list which contains the program to be executed and all its arguments.