subprocess.popen问题 [英] subprocess.popen question

查看:84
本文介绍了subprocess.popen问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修改一个编程示例,我想出了

两个问题......首先是我似乎无法传递

外部命令的参数(我已经能够用

旧模块和cmd是我希望尝试的命令)所有输出

似乎是返回为一行(至少当我在

spe中运行程序时)。


导入子流程
来自os导入系统的


cmd =""" gawk -f altertime.awk -v time_offset = 4 -v

outfile =" testdat.sco" " i1.sco" ""

#subprocess.Popen。

last_line = subprocess.Popen([''gawk.exe''],

stdout = subprocess.PIPE).communicate()[0]

xx = 0

for last_line中的行:

xx = xx + 1

如果xx< 2:

打印行

print str(xx)

I am trying to modify a programming example and I am coming up with
two problems... first is that I can''t seem to pass along the
arguments to the external command (I have been able to do that with
the old module and cmd is the command I wish to try) all the output
seems to be returned as one line (at least when I run the program in
spe).

import subprocess
from os import system
cmd = """gawk -f altertime.awk -v time_offset=4 -v
outfile="testdat.sco" "i1.sco" """
#subprocess.Popen.
last_line = subprocess.Popen([''gawk.exe''],
stdout=subprocess.PIPE).communicate()[0]
xx = 0
for line in last_line:
xx = xx + 1
if xx < 2:
print line
print str(xx)

推荐答案

En Wed,2007年6月20日12:27:47 -0300, Er ********* @ msn。 com

< Er ********* @msn.comescribió:
En Wed, 20 Jun 2007 12:27:47 -0300, Er*********@msn.com
<Er*********@msn.comescribió:

我正在尝试修改一个编程示例,我想出了两个问题......首先是我无法将

参数传递给外部命令(我已经能够用

旧模块和cmd是我想要尝试的命令)所有输出

似乎作为一行返回(至少当我在

spe)中运行程序时。


导入子进程
来自os导入系统的


cmd =""" gawk -f altertime.awk -v time_offset = 4 -v

outfile =" testdat.sco" " i1.sco" ""

#subprocess.Popen。

last_line = subprocess.Popen([''gawk.exe''],

stdout = subprocess.PIPE).communicate()[0]
I am trying to modify a programming example and I am coming up with
two problems... first is that I can''t seem to pass along the
arguments to the external command (I have been able to do that with
the old module and cmd is the command I wish to try) all the output
seems to be returned as one line (at least when I run the program in
spe).

import subprocess
from os import system
cmd = """gawk -f altertime.awk -v time_offset=4 -v
outfile="testdat.sco" "i1.sco" """
#subprocess.Popen.
last_line = subprocess.Popen([''gawk.exe''],
stdout=subprocess.PIPE).communicate()[0]



您构建看似所需的命令行,但执行

gawk.exe代替。

事先更好地拆分参数:

cmd = [" gawk"," -f"," altertime.awk"," ; -v"," time_offset = 4"," -v",

" outfile = testdat.sco"," i1.sco"]

现在,你想对输出做什么?逐行打印?

output = subprocess.Popen(cmd,stdout = subprocess.PIPE).communicate()[0]

lines = output.splitlines()

换行:

打印行

-

Gabriel Genellina


6月20日下午1:46,Gabriel Genellina < gagsl -... @ yahoo.com.ar>

写道:
On Jun 20, 1:46 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:

En Wed,2007年6月20日12:27: 47 -0300,Eric_Dex ... @ msn.com

< Eric_Dex ... @msn.comescribió:
En Wed, 20 Jun 2007 12:27:47 -0300, Eric_Dex...@msn.com
<Eric_Dex...@msn.comescribió:

我正在尝试修改一个编程示例,我想出了两个问题......首先是我无法将

参数传递给外部命令(我已经能够用

旧模块和cmd是我想要尝试的命令)所有输出

似乎作为一行返回(at至少当我在

spe中运行程序时。
I am trying to modify a programming example and I am coming up with
two problems... first is that I can''t seem to pass along the
arguments to the external command (I have been able to do that with
the old module and cmd is the command I wish to try) all the output
seems to be returned as one line (at least when I run the program in
spe).


导入子流程
来自os导入系统的


cmd =""" gawk -f altertime.awk -v time_offset = 4 -v

outfile =" testdat.sco" " i1.sco" ""

#subprocess.Popen。

last_line = subprocess.Popen([''gawk.exe''],

stdout = subprocess.PIPE).communicate()[0]
import subprocess
from os import system
cmd = """gawk -f altertime.awk -v time_offset=4 -v
outfile="testdat.sco" "i1.sco" """
#subprocess.Popen.
last_line = subprocess.Popen([''gawk.exe''],
stdout=subprocess.PIPE).communicate()[0]



你构建看似所需的命令行,但执行

gawk.exe代替。

事先更好地拆分参数:

cmd = [" gawk"," -f"," altertime.awk"," ; -v"," time_offset = 4"," -v",

" outfile = testdat.sco"," i1.sco"]

现在,你想对输出做什么?逐行打印?

output = subprocess.Popen(cmd,stdout = subprocess.PIPE).communicate()[0]

lines = output.splitlines()

换行:

打印行

-

Gabriel Genellina


You build what appears to be the desired command line, but execute
gawk.exe instead.
Better split the arguments beforehand:
cmd = ["gawk", "-f", "altertime.awk", "-v", "time_offset=4", "-v",
"outfile=testdat.sco", "i1.sco"]
Now, what do you want to do with the output? Printing it line by line?
output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
lines = output.splitlines()
for line in lines:
print line

--
Gabriel Genellina



我有一个测试我用cmd替代gawk而且一切都是

代替gawk ..我没有尝试过什么你已经列出了(我是准备去上班的
所以可能要等到tommorow才能尝试

吧)谢谢你的帮助

I had one test where I had cmd in place of gawk and one everything was
in place of gawk.. I haven''t tried what you have listed yet (I am
getting ready to go to work so may have to wait until tommorow to try
it) THanks for the help


6月20日下午1:46,Gabriel Genellina < gagsl -... @ yahoo.com.ar>

写道:
On Jun 20, 1:46 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:

En Wed,2007年6月20日12:27: 47 -0300,Eric_Dex ... @ msn.com

< Eric_Dex ... @msn.comescribió:
En Wed, 20 Jun 2007 12:27:47 -0300, Eric_Dex...@msn.com
<Eric_Dex...@msn.comescribió:

我正在尝试修改一个编程示例,我想出了两个问题......首先是我无法将

参数传递给外部命令(我已经能够用

旧模块和cmd是我想要尝试的命令)所有输出

似乎作为一行返回(at至少当我在

spe中运行程序时。
I am trying to modify a programming example and I am coming up with
two problems... first is that I can''t seem to pass along the
arguments to the external command (I have been able to do that with
the old module and cmd is the command I wish to try) all the output
seems to be returned as one line (at least when I run the program in
spe).


导入子流程
来自os导入系统的


cmd =""" gawk -f altertime.awk -v time_offset = 4 -v

outfile =" testdat.sco" " i1.sco" ""

#subprocess.Popen。

last_line = subprocess.Popen([''gawk.exe''],

stdout = subprocess.PIPE).communicate()[0]
import subprocess
from os import system
cmd = """gawk -f altertime.awk -v time_offset=4 -v
outfile="testdat.sco" "i1.sco" """
#subprocess.Popen.
last_line = subprocess.Popen([''gawk.exe''],
stdout=subprocess.PIPE).communicate()[0]



你构建看似所需的命令行,但执行

gawk.exe代替。

事先更好地拆分参数:

cmd = [" gawk"," -f"," altertime.awk"," ; -v"," time_offset = 4"," -v",

" outfile = testdat.sco"," i1.sco"]

现在,你想对输出做什么?逐行打印?

output = subprocess.Popen(cmd,stdout = subprocess.PIPE).communicate()[0]

lines = output.splitlines()

换行:

打印行

-

Gabriel Genellina


You build what appears to be the desired command line, but execute
gawk.exe instead.
Better split the arguments beforehand:
cmd = ["gawk", "-f", "altertime.awk", "-v", "time_offset=4", "-v",
"outfile=testdat.sco", "i1.sco"]
Now, what do you want to do with the output? Printing it line by line?
output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
lines = output.splitlines()
for line in lines:
print line

--
Gabriel Genellina



C:\ index_tracker \ pipe1.py

Traceback(最近一次调用最后一次):

文件" C:\dex_tracker \pipe1.py",第14行,

last_line = subprocess.Popen([cmd],

stdout = subprocess.PIPE).communicate ()[0]

文件C:\Python25 \lib \ subprocess.py,第593行,在__init__

errread,errwrite)

文件" C:\Python25 \lib \ subprocess.py",第793行,在_execute_child中

startupinfo)

WindowsError:[错误2]系统无法找到指定的文件

脚本已终止。


我可以将其作为批处理文件写出然后运行但是这是一个凌乱

hack。 。

C:\dex_tracker\pipe1.py
Traceback (most recent call last):
File "C:\dex_tracker\pipe1.py", line 14, in
last_line = subprocess.Popen([cmd],
stdout=subprocess.PIPE).communicate()[0]
File "C:\Python25\lib\subprocess.py", line 593, in __init__
errread, errwrite)
File "C:\Python25\lib\subprocess.py", line 793, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
Script terminated.

I can write it out as a batch file and then run it but that is a messy
hack..


这篇关于subprocess.popen问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆