Execute a file with arguments in Python shell

2019-01-02 22:58发布

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?

标签: python shell
10条回答
戒情不戒烟
2楼-- · 2019-01-02 23:54

Besides subprocess.call, you can also use subprocess.Popen. Like the following

subprocess.Popen(['./script', arg1, arg2])

查看更多
我命由我不由天
3楼-- · 2019-01-02 23:55

If you want to run the scripts in parallel and give them different arguments you can do like below.

import os
os.system("python script.py arg1 arg2 & python script.py arg11 arg22")
查看更多
淡お忘
4楼-- · 2019-01-02 23:57

Actually, wouldn't we want to do this?

import sys
sys.argv = ['abc.py','arg1', 'arg2']
execfile('abc.py')
查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-01-03 00:02

You can't pass command line arguments with execfile(). Look at subprocess instead.

查看更多
登录 后发表回答