简体   繁体   English

如何在python脚本中将随机参数传递给另一个python程序?

[英]how to pass random arguments to another python program in python script?

I am just trying to pass random arguments for below python script. 我只是想为下面的python脚本传递随机参数。

Code: 码:

import json,sys,os,subprocess

arg1 = 'Site1'
arg2 = "443"
arg3 = 'admin@example.com'
arg4 = 'example@123'
arg5 = '--output req.txt'
arg6 = '-h'

obj=json.load(sys.stdin)

for i in range(len(obj['data'])):
    print obj['data'][i]['value']
    subprocess.call(['./malopinfo.py', arg1, arg2, arg3, arg4, obj , arg5])

In above code variable obj will change randomly, But apart from that all arguments are static. 在上面的代码中,变量obj将随机改变,但除此之外,所有参数都是静态的。

Error: 错误:

root@prabhu:/home/teja/MalopInfo/version1/MalopInfo# ./crver1.sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   116    0   116    0     0   5313      0 --:--:-- --:--:-- --:--:--  5523
11.945403842773683082
Traceback (most recent call last):
  File "qjson.py", line 15, in <module>
    subprocess.call(['./malopinfo.py', arg1, arg2, arg3, arg4, obj])
  File "/usr/lib/python2.7/subprocess.py", line 522, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1335, in _execute_child
    raise child_exception

I am trying to execute is 我想要执行的是

python malopinfo.py Site1 443 admin@example.com example@123 11.945403842773683082 --output req.txt

Please help me on this. 请帮帮我。

It looks to me like you're passing the entire obj dictionary into the command. 在我看来,你将整个obj字典传递给命令。 To get the desired invocation, pass obj['data'][i]['value'] in the arguments list to subprocess.call . 要获得所需的调用,请将参数列表中的obj['data'][i]['value']传递给subprocess.call So, the final line of your script should be 所以,你的脚本的最后一行应该是

    subprocess.call(['./malopinfo.py', arg1, arg2, arg3, arg4, obj['data'][i]['value'], arg5])

Or, you can make a variable to contain that on each loop iteration, whatever works. 或者,您可以创建一个变量来包含每个循环迭代中的变量,无论什么工作。

You are directly passing an object. 您正在直接传递一个对象。 Beforehand you need to convert that into string as subprocess.call will expect obj to be a string. 之前你需要将其转换为字符串,因为subprocess.call会期望obj是一个字符串。 Get the string value of one of the obj properties like you already have done obj['data'][i]['value'] and pass it into your subprocess.call . 获取其中一个obj属性的字符串值,就像你已经完成了obj['data'][i]['value']并将它传递给你的subprocess.call

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM