Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-使用Popen中的列表作为命令_Python_String_List_Subprocess_Popen - Fatal编程技术网

Python-使用Popen中的列表作为命令

Python-使用Popen中的列表作为命令,python,string,list,subprocess,popen,Python,String,List,Subprocess,Popen,我尝试用Popen创建一个子流程。以下是我的代码: hostname = 'host' servername = 'server' commandargs = ['/usr/sbin/mminfo',' -o n',' -s',servername,' -q "client=\'',hostname,'\',savetime>=last day"',' -r "client,name"'] process = subprocess.Popen(commandargs, stdout=sub

我尝试用Popen创建一个子流程。以下是我的代码:

hostname = 'host'
servername = 'server'
commandargs = ['/usr/sbin/mminfo',' -o n',' -s',servername,' -q "client=\'',hostname,'\',savetime>=last day"',' -r "client,name"']
process = subprocess.Popen(commandargs, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
问题是执行的命令失败,并显示一条消息,即联系的服务器不可用。似乎没有使用变量hostname。。。 尝试使用字符串而不是列表执行相同的操作,就像Popen中的命令(Shell=True)一样,一切正常

有人知道代码有什么问题吗

问候。
Stefan

在列表中,每个参数都必须是自己的项。字符串中不应有空格:

commandargs = ['/usr/sbin/mminfo', '-o', 'n', '-s', servername,
               '-q', "client='" + hostname + "',savetime>=last day",
               '-r', 'client,name']

给定列表中的每个字符串都作为单个命令行参数处理。使用此语法时也不需要使用引号

试着这样做:

hostname = 'host'
servername = 'server'
commandargs = [
    '/usr/sbin/mminfo',
    '-o', 'n', # these are separate arguments, but on the same line for clarity's sake
    '-s', servername, # same here
    '-q', "client='%s',savetime>=last day" % hostname, # same here...
    '-r', 'client,name' # and here.
]
process = subprocess.Popen(commandargs, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
编辑:或者,根据评论,类似于

import subprocess

client_name = "lxds05"
server_name = "nsr_srv"

queryspec = "client='%s',savetime>=last day" % client_name
reportspec = "client,name,savetime(17),nsavetime,level,ssflags"

args = [
    '/usr/sbin/mminfo',
    '-o', 'n',
    '-s', server_name,
    '-q', queryspec,
    '-r', reportspec,
    '-x', 'c'
]

subprocess.Popen(args) # ... etc

不要引用主机名,不需要。有效的主机名不包含任何可能扩展为奇怪内容的字符,也不包含空格。您应该做的另一件事是将命令中的每个实体分开,不要执行'-o-n',而是'-o',n',等等。hostname='host'您的意思是^?这是我用测试主机名填充变量的测试。commandargs=['/usr/sbin/mminfo'、'-o'、'n'、'-s',servername'-q'、'-client='',hostname'\'、savetime>=last'、''day'、'-r'、'-client、name、savetime(17)、nsavetime、level、ssflags'-x'、'c;'、'-sed'、'1d']如果没有
shell
参数,使用管道将无法工作,但是如果您只使用
sed
删除一行,您也可以在Python中这样做。对于我的粘贴,很抱歉,:-(,我尝试将编辑过的列表放在这里。好的,非常感谢。但是如何在列表元素中使用空白?
something=[“”,“”,“”]
将创建一个包含三个空字符串的列表。谢谢,这很好。只有“最后一天”其中有一个空格。是否有一个特殊字符?@StefanS否,列表的目的正是让子进程执行所有转义。好的,像“我必须转义它吗?”这样的字符是什么?不。正如我所写的,子进程为命令行调用执行所有转义。如果您的程序将其参数解释为对于复合结构(如示例中的
-q
,我之前没有注意到它-更新),您必须自己组装该表示。