加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

subprocess模块(了解)

发布时间:2020-12-20 10:51:44 所属栏目:Python 来源:网络整理
导读:一、subprocess模块 subprocess模块允许你去创建一个新的进程让其执行另外的程序,并与它进行通信,获取标准的输入、标准输出、标准错误以及返回码等。更多查看官网:https://docs.python.org/2/library/subprocess.html?highlight=subprocess#frequently-us

一、subprocess模块

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$‘,stdin=res1.stdout,stdout=subprocess.PIPE) print(res.stdout.read().decode(‘utf-8‘)) # 等同于上面,但是上面的优势在于,一个数据流可以和另外一个数据流交互,可以通过爬虫得到结果然后交给grep res1 = subprocess.Popen(‘ls /Users/jieli/Desktop |grep txt$‘,stdout=subprocess.PIPE) print(res1.stdout.read().decode(‘utf-8‘)) # windows下: # dir | findstr ‘test*‘ # dir | findstr ‘txt$‘ res1 = subprocess.Popen(r‘dirC:UsersAdministratorPycharmProjectstest函数备课‘,stdout=subprocess.PIPE) res = subprocess.Popen(‘findstr test*‘,stdout=subprocess.PIPE) # subprocess使用当前系统默认编码,得到结果为bytes类型,在windows下需要用gbk解码 print(res.stdout.read().decode(‘gbk‘))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读