在 Python shell 中执行带参数的文件 [英] Execute a file with arguments in Python shell
本文介绍了在 Python shell 中执行带参数的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 Python Shell 中运行一个命令来执行一个带参数的文件.
I would like to run a command in Python Shell to execute a file with an argument.
例如:execfile("abc.py")
但是如何添加 2 个参数?
For example: execfile("abc.py")
but how to add 2 arguments?
推荐答案
execfile
运行 Python 文件,但通过加载它,而不是作为脚本.只能传入变量绑定,不能传入参数.
execfile
runs a Python file, but by loading it, not as a script. You can only pass in variable bindings, not arguments.
如果您想在 Python 中运行程序,请使用 subprocess.call
.例如
If you want to run a program from within Python, use subprocess.call
. E.g.
import subprocess
subprocess.call(['./abc.py', arg1, arg2])
这篇关于在 Python shell 中执行带参数的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文