Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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
带有2个变量的python空白问题_Python_Variables_Scripting_Whitespace - Fatal编程技术网

带有2个变量的python空白问题

带有2个变量的python空白问题,python,variables,scripting,whitespace,Python,Variables,Scripting,Whitespace,在运行Python 2.6.6时,每当我尝试使用另一个变量中的两个变量时,就会出现空白错误: 'C:\Program' is not recognized as an internal or external command, operable program or batch file. 这是我的代码,问题在于cmd变量: from subprocess import call, Popen, PIPE, STDOUT example = '"C:\\Program Files\\Exam

在运行Python 2.6.6时,每当我尝试使用另一个变量中的两个变量时,就会出现空白错误:

'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
这是我的代码,问题在于cmd变量:

from subprocess import call, Popen, PIPE, STDOUT

example = '"C:\\Program Files\\Example\\test.cmd"'  
output = '"C:\\test\\python\\reportFromPython.xml"'

cmd = example + " -T 'testing title' " + output

p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
output = p.stdout.read()
print output
如果我改变

cmd = example + " -T 'testing title' " + output

然后它工作,但我需要的输出部分。。。如何使它同时使用这两个变量?

根据,如果运行.cmd文件,则不需要
shell=True
。然后可以将参数作为列表传递:

cmd = [example, "-T", "'testing title'", output]

除了由于shell扩展而删除的
shell=True

之外,其余代码都是相同的,请使用内部
子流程
工具为您进行参数转义,即
Popen([示例,“-T”,“测试标题”,输出),stdin=PIPE,stdout=PIPE,stderr=PIPE)
.Hmm。。这对我还是不起作用。删除shell=True时会出现以下错误:回溯(上次调用):文件“C:/test/python/062818.py”,第15行,p=Popen(cmd,stdin=PIPE,stdout=PIPE,stderr=stdout)文件“C:\Python26\lib\subprocess.py”,第623行,init errread,errwrite)文件“C:\Python26\lib\subprocess.py”,第833行,在_execute_child startupinfo)WindowsError:[Error 5]中,访问被拒绝。此外,在使用括号更改变量时,我会得到以下信息:文件名、目录名或卷标语法不正确。如果我只是尝试cmd=我会发现:“\'C:\Program Files\Example\test.cmd\'”不被识别为内部或外部命令、可操作的程序或批处理文件。因此,我使用类似的东西来实现它:Popen([cmd,“-T”,“testing title”,output],stdin=PIPE,stdout=PIPE,stderr=PIPE),但在此之前,我必须创建一个名为cmd=的新变量,否则它将无法工作。。。现在,当我的脚本运行时,cmd窗口会显示出来,而在它在幕后运行之前,有没有办法解决这个问题?这与shell=true参数有关吗?!
cmd = [example, "-T", "'testing title'", output]