[英]How would you pass arguments to a script being passed as an argument?
Basically the title, I don't know how to pass an argument to a script that is being passed as an argument in a shell command. 基本上是标题,我不知道如何将参数传递给在shell命令中作为参数传递的脚本。 Take the command below for example:
以下面的命令为例:
$someexe --arg-for-someexe scriptA.py $ someexe --arg-for-someexe脚本
somexe is going to read from scriptA and perform a bunch of tasks and then spit out some data into a destination file hard-coded into scriptA. somexe将要从scriptA中读取并执行一堆任务,然后将一些数据吐出到硬编码到scriptA中的目标文件中。 How would I pass the destination file into scriptA as a command line argument?
如何将目标文件作为命令行参数传递给scriptA? In python I know I can get arguments using sys.argv[i] but someexe will also try to grab those arguments and could cause problems.
在python中,我知道我可以使用sys.argv [i]来获取参数,但是someexe还将尝试获取这些参数并可能导致问题。 How could I accomplish this?
我该怎么做?
You could do something like that: 您可以这样做:
Caller script (scripta.py): 呼叫者指令码(scripta.py):
import os
def main(args):
os.system('python scriptb.py' + ' ' + str(args))
if __name__ == '__main__':
import sys
args = sys.argv[1:]
main(args)
Receiver script (scriptb.py): 接收方脚本(scriptb.py):
if __name__ == '__main__':
import sys
args = sys.argv[1:]
print(args)
To call script a - say in a terminal: 调用脚本-在终端中说:
python scripta.py arg1 42 arg2 43
Will print in scriptb.py terminal: 将在scriptb.py终端中打印:
['[arg1,', '42,', 'arg2,', '43]']
os.system() spawns it in a separate shell/process. os.system()在单独的shell /进程中生成它。 May or not be what you want.
可能不是您想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.