简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM