Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subprocess pipes stdin without using files

I've got a main process in which I run a subprocess, which stdin is what I want to pipe. I know I can do it using files:

import subprocess
subprocess.call('shell command', stdin=open('somefile','mode'))

Is there any option to use a custom stdin pipe WITHOUT actual hard drive files? Is there any option, for example, to use string list (each list element would be a newline)?

I know that python subprocess calls .readline() on the pipe object.

like image 406
ducin Avatar asked Aug 04 '13 19:08

ducin


1 Answers

First, use subprocess.Popen - .call is just a shortcut for it, and you'll need to access the Popen instance so you can write to the pipe. Then pass subprocess.PIPE flag as the stdin kwarg. Something like:

import subprocess
proc = subprocess.Popen('shell command', stdin=subprocess.PIPE)
proc.stdin.write("my data")

http://docs.python.org/2/library/subprocess.html#subprocess.PIPE

like image 174
AdamKG Avatar answered Oct 04 '22 02:10

AdamKG