首页 > 其他 > 详细

subprocess模块(了解)

时间:2019-05-04 11:52:08      阅读:117      评论:0      收藏:0      [点我收藏+]

subprocess模块(了解)

控制子进程,更多查看官网:https://docs.python.org/2/library/subprocess.html?highlight=subprocess#frequently-used-arguments

```python
[TOC]

subprocess模块(了解)

控制子进程,更多查看官网:https://docs.python.org/2/library/subprocess.html?highlight=subprocess#frequently-used-arguments

import subprocess
import subprocess

'''
sh-3.2# ls /Users/nick/Desktop |grep txt$
mysql.txt
tt.txt
事物.txt
'''

res1 = subprocess.Popen('ls /Users/jieli/Desktop',
                        shell=True, stdout=subprocess.PIPE)
res = subprocess.Popen('grep txt$', shell=True, stdin=res1.stdout,
                       stdout=subprocess.PIPE)

print(res.stdout.read().decode('utf-8'))


# 等同于上面,但是上面的优势在于,一个数据流可以和另外一个数据流交互,可以通过爬虫得到结果然后交给grep
res1 = subprocess.Popen('ls /Users/jieli/Desktop |grep txt$',
                        shell=True, stdout=subprocess.PIPE)
print(res1.stdout.read().decode('utf-8'))


# windows下:
# dir | findstr 'test*'
# dir | findstr 'txt$'
res1 = subprocess.Popen(
    r'dirC:\Users\Administrator\PycharmProjects\test\函数备课', shell=True, stdout=subprocess.PIPE)
res = subprocess.Popen('findstr test*', shell=True, stdin=res1.stdout,
                       stdout=subprocess.PIPE)

# subprocess使用当前系统默认编码,得到结果为bytes类型,在windows下需要用gbk解码
print(res.stdout.read().decode('gbk'))

subprocess模块(了解)

原文:https://www.cnblogs.com/nickchen121/p/10807985.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!