在Linux上,我使用supbprocess.Popen來運行應用程序。該應用程序的命令行需要輸入文件的路徑。我瞭解到我可以將路徑/ dev/stdin傳遞到命令行,然後使用Python的subproc.stdin.write()將輸入發送到子進程。你能打開stdin作爲MS Windows上的Python文件嗎?
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)
還有這個應用程序的MS Windows版本。在MS Windows上有相當於使用/ dev/stdin還是Linux(Posix)特定解決方案?
相關:[視窗相當於「/dev/stdin"?](http://stackoverflow.com/questions/7395157/windows-equivalent-to-dev-stdin) –