Can you open stdin as a file on MS Windows in Pyth

2019-06-28 05:29发布

问题:

On Linux, I'm using supbprocess.Popen to run an app. The command line of that app requires a path to an input file. I learned I can pass the path /dev/stdin to the command line, and then use Python's subproc.stdin.write() to send input to the subprocess.

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)

There's also an MS Windows version of this app. Is there an equivalent on MS Windows to using the /dev/stdin or is the a Linux (Posix) specific solution?

回答1:

If myapp treats - as a special filename that denotes stdin then:

from subprocess import PIPE, Popen

p = Popen(['/path/to/myapp', '-i', '-'], stdin=PIPE, stdout=PIPE)
stdout, _ = p.communicate('\n'.join(inbuff).encode('utf-8'))
outbuff = stdout.decode('utf-8').splitlines()

If you can't pass - then you could use a temporary file:

import os
import tempfile

with tempfile.NamedTemporaryFile(delete=False) as f:
     f.write('\n'.join(inbuff).encode('utf-8'))

p = Popen(['/path/to/myapp', '-i', f.name], stdout=PIPE)
outbuff, conditionbuff = [], []
for line in iter(p.stdout.readline, ''):
    line = line.strip().decode('utf-8')
    if 'condition' in line:
        conditionbuff.append(line)
    else:
        outbuff.append(line)
p.stdout.close()
p.wait()
os.remove(f.name) #XXX add try/finally for proper cleanup

To suppress stderr you could pass open(os.devnull, 'wb') as stderr to Popen.



回答2:

You shouldn't use '/dev/stdin' but sys.stdin



回答3:

Total shot in the dark, but con: is the name of the console device (you know, good ol' copy con newfile.txt from DOS). You might be able to pass con: as an argument to the program that won't accept '-'.



回答4:

Please, use

import sys

sys.stdout.write("Abc\n123\n")

or

for line in sys.stdin.readlines():

... print line

for example