Execute a file with arguments in Python shell


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?

1/16/2017 3:59:51 PM

Accepted Answer

execfile runs a Python file, but by loading it, not as a script. You can only pass in variable bindings, not arguments.

If you want to run a program from within Python, use subprocess.call. E.g.

subprocess.call(['./abc.py', arg1, arg2])
4/26/2011 11:22:51 AM

try this:

import sys
sys.argv = ['arg1', 'arg2']

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow