python 3.8.2 shell_Python获取shell管道数据/输入的2种方法: subprocess子 ...- …

本文介绍了Python通过subprocess模块与shell命令、其他程序进行交互,包括无交互的简单调用、获取输出、输入与输出的交互,并给出多个示例,强调使用Popen时需要清空缓冲区的重要性。
摘要由CSDN通过智能技术生成

在很久以前,我写了一个系列,Python和C和C++的交互,如下

http://blog.csdn.net/marising/archive/2008/08/28/2845339.aspx

目的是解决Python和C/C++的互操作性的问题,假如性能瓶颈的地方用C来写,而一些外围工作用Python来完成,岂不是完美的结合。

今天发现了更方便的方式,就是用subprocess模块,创建子进程,然后用管道来进行交互,而这种方式在shell中非常普遍,比如:cat xxx.file | test.py 就是用的管道,另外,在hadoop中stream模式就是用的管道。

其实在python中,和shell脚本,其他程序交互的方式有很多,比如:

os.system(cmd),os.system只是执行一个shell命令,不能输入、且无返回

os.open(cmd),可以交互,但是是一次性的,调用都少次都会创建和销毁多少次进程,性能太差

所以,建议用subprocess,但是subprocess复杂一些,可以参考python docs:

http://docs.python.org/library/subprocess.html

先看一个简单的例子,调用ls命令,两者之间是没有交互的:

[python] view plaincopy

import subprocess

p = subprocess.Popen('ls')

再看在程序中获取输出的例子:

[c-sharp] view plaincopy

import subprocess

p = subprocess.Popen('ls',stdout=subprocess.PIPE)

print p.stdout.readlines()

再看看有输入,有输出的例子,父进程发送'say hi',子进程输出 test say hi,父进程获取输出并打印

[python] view plaincopy

#test1.py

import sys

line = sys.stdin.readline()

print 'test',line

#run.py

from subprocess import *

p =Popen('./test1.py',stdin=PIPE,stdout=PIPE)

p.stdin.write('say hi/n')

print p.stdout.readline()

#result

test say hi

看看连续输入和输出的例子

test.py

[python] view plaincopy

import sys

while True:

line = sys.stdin.readline()

if not line:break

sys.stdout.write(line)

sys.stdout.flush()

run.py

[python] view plaincopy

import sys

from subprocess import *

proc = Popen('./test.py',stdin=PIPE,stdout=PIPE,shell=True)

for line in sys.stdin:

proc.stdin.write(line)

proc.stdin.flush()

output = proc.stdout.readline()

sys.stdout.write(output)

注意,run.py的flush和test.py中的flush,要记得清空缓冲区,否则程序得不到正确的输入和输出

C/C++的类似,伪代码如下

[cpp] view plaincopy

char* line = new char[2048];

while (fgets(line, 2028, stdin)) {

printf(line);

fflush(stdout);//必须清空缓冲区

}

Popen其他参数的设置,请参考python docs。

在很久以前,我写了一个系列,Python和C和C++的交互,如下

http://blog.csdn.net/marising/archive/2008/08/28/2845339.aspx

目的是解决Python和C/C++的互操作性的问题,假如性能瓶颈的地方用C来写,而一些外围工作用Python来完成,岂不是完美的结合。

今天发现了更方便的方式,就是用subprocess模块,创建子进程,然后用管道来进行交互,而这种方式在shell中非常普遍,比如:cat xxx.file | test.py 就是用的管道,另外,在hadoop中stream模式就是用的管道。

其实在python中,和shell脚本,其他程序交互的方式有很多,比如:

os.system(cmd),os.system只是执行一个shell命令,不能输入、且无返回

os.open(cmd),可以交互,但是是一次性的,调用都少次都会创建和销毁多少次进程,性能太差

所以,建议用subprocess,但是subprocess复杂一些,可以参考python docs:

http://docs.python.org/library/subprocess.html

先看一个简单的例子,调用ls命令,两者之间是没有交互的:

[python] view plaincopy

import subprocess

p = subprocess.Popen('ls')

再看在程序中获取输出的例子:

[c-sharp] view plaincopy

import subprocess

p = subprocess.Popen('ls',stdout=subprocess.PIPE)

print p.stdout.readlines()

再看看有输入,有输出的例子,父进程发送'say hi',子进程输出 test say hi,父进程获取输出并打印

#test1.py

import sys

line = sys.stdin.readline()

print 'test',line

#run.py

from subprocess import *

p =Popen('./test1.py',stdin=PIPE,stdout=PIPE)

p.stdin.write('say hi/n')

print p.stdout.readline()

#result

test say hi

看看连续输入和输出的例子

test.py

[python] view plaincopy

import sys

while True:

line = sys.stdin.readline()

if not line:break

sys.stdout.write(line)

sys.stdout.flush()

run.py

[python] view plaincopy

import sys

from subprocess import *

proc = Popen('./test.py',stdin=PIPE,stdout=PIPE,shell=True)

for line in sys.stdin:

proc.stdin.write(line)

proc.stdin.flush()

output = proc.stdout.readline()

sys.stdout.write(output)

注意,run.py的flush和test.py中的flush,要记得清空缓冲区,否则程序得不到正确的输入和输出

C/C++的类似,伪代码如下

[cpp] view plaincopy

char* line = new char[2048];

while (fgets(line, 2028, stdin)) {

printf(line);

fflush(stdout);//必须清空缓冲区

}

末了,我说一句,其实如果只是shell脚本调用单行python命令或脚本

亦或者如 hadoop 中stream调用 python -c 命令行(因为hadoop stream

中不能出现 $ 符号,所以一般不能用awk、sed、shell等命令),

so,我们需要更简洁点的 one line command:

Use sys.stdin to read the input . Example :

Example content of s.py :

import sys

data=sys.stdin.readlines()print data

-------------------------------------- Running :

user@xxxxxxx:~$ cat t.txt

alpha

beta

gamma

user@xxxxxxx:~$ cat t.txt|python./s.py['alpha\n','beta\n','gamma\n']

You can also make the python script as shell script using this Shebang :

#!/usr/bin/env python

and changing permission to 'a+x'

user@xxxxxxx:~$ cat t.txt|./s.py['alpha\n','beta\n','gamma\n']

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值