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?
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
.
You shouldn't use '/dev/stdin'
but sys.stdin
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 '-'.
Please, use
import sys
sys.stdout.write("Abc\n123\n")
or
for line in sys.stdin.readlines():
... print line
for example