I would like to run a command in Python Shell to execute a file with an argument.
For example: execfile("abc.py")
but how to add 2 arguments?
You can use the command line arguments by using the sys. argv[] array. The first index of the array consists of the python script file name. And from the second position, you'll have the command line arguments passed while running the python script.
To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!
try this:
import sys sys.argv = ['arg1', 'arg2'] execfile('abc.py')
Note that when abc.py
finishes, control will be returned to the calling program. Note too that abc.py
can call quit()
if indeed finished.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With