且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python Popen 发送到标准输入处理,接收标准输出

更新时间:2023-11-18 08:00:52

一个工作示例

#!/usr/bin/env python
import subprocess
text = 'hello'
proc = subprocess.Popen(
    'md5sum',stdout=subprocess.PIPE,
    stdin=subprocess.PIPE)
proc.stdin.write(text)
proc.stdin.close()
result = proc.stdout.read()
print result
proc.wait()

获得与executable <params.file >output.file",这样做:

#!/usr/bin/env python
import subprocess
infile,outfile = 'params.file','output.file'
with open(outfile,'w') as ouf:
    with open(infile,'r') as inf:
        proc = subprocess.Popen(
            'md5sum',stdout=ouf,stdin=inf)
        proc.wait()