我有一个正在运行的脚本(使用我编写的模块包),某些脚本在某些部分使用命令行参数执行另一个脚本。我想要另一个脚本(这是暴露给其他人,因为其他人不是,但这个是可修改的&用户可查看)自动传输sys.argv[1]
到某个功能,如foo()
,它有一些论点,用户发送时调用它 - 但我不希望用户知道额外的参数,并需要自己发送(换句话说,我想sys.argv[1]
自动发送到foo()
)。将命令行参数作为默认函数参数
这可能吗?
例子:
#my_script.py#
import subprocess
def do_stuff():
#stuff
return calculated_var
my_var = do_stuff()
subprocess.check_call(["C:/path/to/user/script/user_script.py", str(my_var)])
#user_script.py#
import my_module
print "My script was finally called, I don't know about any arguments"
my_module.my_func(my_var1, my_var2) #In the background, both my_var1 and my_var 2 are called as function arguments BUT also sys.argv[1] without the user knowing
#my_module.py#
def my_func(arg1, arg2, arg3="""WHAT DO I PUT HERE? I can't put sys.argv[1]"""):
pass
很难跟踪你的问题。请用如下例子重写它:我有script.py并用命令行参数调用它,并且我有包含函数'foo(...)'的script2.py,我需要这样做和那个。 – vrs
@vrs完成。它足够吗? – Jim
为什么不能使用'sys.argv [1]'?它会工作。 – Dunes