在Linux上,我使用超级进程。Popen运行应用程序。该应用程序的命令行需要输入文件的路径。我了解到可以将路径/dev/stdin传递到命令行,然后使用Python的subproc.stdin.write()将输入发送到子进程。在
import subprocess
kw['shell'] = False
kw['executable'] = '/path/to/myapp'
kw['stdin'] = subprocess.PIPE
kw['stdout'] = subprocess.PIPE
kw['stderr'] = subprocess.PIPE
subproc = subprocess.Popen(['','-i','/dev/stdin'],**kw)
inbuff = [u'my lines',u'of text',u'to process',u'go here']
outbuff = []
conditionbuff = []
def processdata(inbuff,outbuff,conditionbuff):
for i,line in enumerate(inbuff):
subproc.stdin.write('%s\n'%(line.encode('utf-8').strip()))
line = subproc.stdout.readline().strip().decode('utf-8')
if 'condition' in line:
conditionbuff.append(line)
else:
outbuff.append(line)
processdata(inbuff,outbuff,conditionbuff)
这个应用还有一个微软Windows版本。在MS Windows上是否有与使用/dev/stdin等效的方法,或者是Linux(Posix)特定的解决方案?在
如果
myapp
将-
视为表示stdin的特殊文件名,则:如果无法传递
^{pr2}$-
,则可以使用临时文件:为了抑制
stderr
,可以将open(os.devnull, 'wb')
作为stderr
传递给Popen
。在完全是瞎猜的,但是
con:
是控制台设备的名称(你知道,DOS中的好ol'copy con newfile.txt
)。您可以将con:
作为参数传递给不接受“-”的程序。在请使用
或者
例如
相关问题 更多 >
编程相关推荐