Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute a file with arguments in Python shell

Tags:

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?

like image 286
olidev Avatar asked Apr 26 '11 10:04

olidev


People also ask

How do I run a Python file in Terminal with 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.

How do I run a file in Python shell?

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!


1 Answers

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.

like image 109
buggywhip Avatar answered Sep 28 '22 04:09

buggywhip