资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

subprocess模块的使用-创新互联

subprocess模块的使用

网站建设哪家好,找成都创新互联!专注于网页设计、网站建设、微信开发、微信小程序定制开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了周宁免费建站欢迎大家使用!

1、调用本地shell脚本

[root@133 systeminformation]# cat test.sh
#!/bin/bash
echo "hello world!"

In [1]: from subprocess import Popen,PIPE
In [2]: p=Popen('./test.sh',shell=True)
hello world!

2、p1的输出作为p2的输入(PIPE即是管道)

In [15]: p1 = Popen(['ls'],stdout=PIPE)

In [16]: p2 = Popen(['grep','py'],stdin=p1.stdout,stdout=PIPE)

In [17]: result = p2.stdout

In [18]: for i in result:print i
10_httpd.py

1_md5.py

1_walk.py

ErrorExcept.py

hashlib2.py

isNum.py

process_check_call.py

subprocess.py

subprocess.pyc

top10.py

walk1.py

yield1.py

yield2.py

 p.communicate()方法相当于p.stdin.write()、p.stdin.close()和p.stdout.read()这3个方法

p.communicate返回的是两个元素的元祖

In [29]: p = Popen(['cat'],stdin=PIPE,stdout=PIPE)

In [30]: p.communicate('abc')
Out[30]: ('abc', None)

In [19]: p = Popen(['wc'],stdin=PIPE,stdout=PIPE,stderr=PIPE)
In [20]: p.terminate()
In [21]: p.wait()
Out[21]: -15
In [22]: p.wait()
Out[22]: -15
In [23]: p.wait()
Out[23]: -15
In [24]: p.pid
Out[24]: 6241
In [25]: p.returncode
Out[25]: -15



In [43]: help(p1.wait)

Help on method wait in module subprocess:

wait(self) method of subprocess.Popen instance
    Wait for child process to terminate.  Returns returncode
    attribute.
~


In [44]: help(p1.returncode)

Help on NoneType object:

class NoneType(object)
 |  Methods defined here:
 |  
 |  __hash__(...)
 |      x.__hash__() <==> hash(x)
 |  
 |  __repr__(...)
 |      x.__repr__() <==> repr(x)
(END)

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前名称:subprocess模块的使用-创新互联
本文网址:http://cdkjz.cn/article/dehehp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220