import  subprocess
'''
sh-3.2# ls /Users/egon/Desktop |grep txt$
mysql.txt
tt.txt
事物.txt
'''
#1 Linux下,通过python运行终端代码:
res1=subprocess.Popen('ls /Users/jieli/Desktop',
shell=True,
stdout=subprocess.PIPE)
res=subprocess.Popen('grep txt$',
shell=True,
stdin=res1.stdout, # res1.stdout是res.stdin
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
print(res.stdout.read().decode('utf-8'))
print(res.stderr.read().decode('utf-8'))
#等同于上面,但是上面的优势在于,一个数据流可以和另外一个数据流交互,可以通过爬虫得到结果然后交给grep
res1=subprocess.Popen('ls /Users/jieli/Desktop |grep txt$',
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
print(res1.stdout.read().decode('utf-8'))
print(res.stderr.read().decode('utf-8')) #2 windows下,通过python运行终端代码:
# dir | findstr 'test*'
# dir | findstr 'txt$'
import subprocess
res1=subprocess.Popen(r'dir C:\Users\Administrator\PycharmProjects\test\函数备课',shell=True,stdout=subprocess.PIPE)
res=subprocess.Popen('findstr test*',shell=True,stdin=res1.stdout,
stdout=subprocess.PIPE,stderr=subprocess.PIPE) print(res.stdout.read().decode('gbk'))
print(res.stderr.read().decode('gbk')) #subprocess使用当前系统默认编码,得到结果为bytes类型,在windows下需要用gbk解码

subprocess 模块 与终端相互交互的更多相关文章

  1. 18 subprocess模块(跟操作系统交互)

    1.基本概念介绍 我们经常需要通过Python去执行一条系统命令或脚本,系统的shell命令是独立于你的python进程之外的, 每执行一条命令,就是发起一个新进程,通过python调用系统命令或脚本 ...

  2. Python学习笔记——基础篇【第六周】——Subprocess模块

    执行系统命令 可以执行shell命令的相关模块和函数有: os.system os.spawn* os.popen*          --废弃 popen2.*           --废弃 com ...

  3. Python第十一天 异常处理 glob模块和shlex模块 打开外部程序和subprocess模块 subprocess类 Pipe管道 operator模块 sorted函数 os模块 hashlib模块 platform模块 csv模块

    Python第十一天    异常处理  glob模块和shlex模块    打开外部程序和subprocess模块  subprocess类  Pipe管道  operator模块   sorted函 ...

  4. Python之路(第二十篇) subprocess模块

    一.subprocess模块 subprocess英文意思:子进程 那什么是进程呢? (一)关于进程的相关理论基础知识 进程是对正在运行程序的一个抽象,进程的概念起源于操作系统,是操作系统最核心的概念 ...

  5. [xml模块、hashlib模块、subprocess模块、os与sys模块、configparser模块]

    [xml模块.hashlib模块.subprocess模块.os与sys模块.configparser模块] xml模块 XML:全称 可扩展标记语言,为了能够在不同的平台间继续数据的交换,使交换的数 ...

  6. os、sys、json、subprocess模块

    今日内容概要 1.os模块 2.sys模块 3.json模块 4.subprocess模块 今日内容详细 os模块 """该模块主要是跟操作系统打交道"&quo ...

  7. os、json、sys、subprocess模块

    os模块 import os 1.创建目录(文件夹) os.mkdir(r'a') # 相对路径 只能创建单级目录 os.makedirs(r'a\b') # 可以创建单级和多及目录 2.删除目录 o ...

  8. os模块,sys模块,json模块,subprocess模块

    os模块 一·什么是os模块 os模块提供了多数操作系统的功能接口函数.当os模块被导入后,它会自适应于不同的操作系统平台,根据不同 的平台进行相应的操作,在python编程时,经常和文件.目录打交道 ...

  9. python学习道路(day7note)(subprocess模块,面向对象)

    1.subprocess模块   因为方法较多我就写在code里面了,后面有注释 #!/usr/bin/env python #_*_coding:utf-8_*_ #linux 上调用python脚 ...

随机推荐

  1. 【Objective-C】06-点语法

    前言 在Java中,我们可以通过"对象名.成员变量名"来访问对象的公共成员变量,这个就称为"点语法".比如: 1.在Student类的第2行定义了一个公共的成员 ...

  2. Ajax同步与异步优缺点与使用

    一.什么是同步请求:(false)       同步请求即是当前发出请求后,浏览器什么都不能做,必须得等到请求完成返回数据之后,才会执行后续的代码,相当于是排队,前一个人办理完自己的事务,下一个人才能 ...

  3. 本地运行storm时报错

    java.lang.NoClassDefFoundError: backtype/storm/topology/IRichSpout at java.lang.Class.getDeclaredMet ...

  4. A protocol error occurred. Change of username or service not allowed: (root,ssh-connection) -> (zoujiaqing,ssh-connection)

    SecureCRT ssh 客户端连接失败: The server has disconnected with an error.  Server message reads: A protocol ...

  5. CAP定理(原则)以及BASE理论

    CAP定理(原则)以及BASE理论 CAP定理(原则)概念 CAP原则又称CAP定理,指的是在一个分布式系统中, Consistency(一致性). Availability(可用性).Partiti ...

  6. python zlib压缩存储到mysql列

    数据太大压缩存储,可以使用zlib中的压缩函数,代码如下: import ujson import MySQLdb import zlib import base64 kwargs = { 'host ...

  7. 【BZOJ】1665: [Usaco2006 Open]The Climbing Wall 攀岩(spfa)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1665 这题只要注意到“所有的落脚点至少相距300”就可以大胆的暴力了. 对于每个点,我们枚举比他的x ...

  8. Eclipse中的build path详解

    http://blog.csdn.net/qqqqqq654/article/details/53043742

  9. numpy 和 pandas 中常用的一些函数及其参数

    numpy中有一些常用的用来产生随机数的函数,randn()和rand()就属于这其中. numpy.random.randn(d0, d1, …, dn)是从标准正态分布中返回一个或多个样本值.  ...

  10. 第八篇:文件共享和使用 dup 函数创建新描述符的区别

    前言 文件共享是指同时打开一个文件 用 dup 函数能对指定文件描述符再创建一个新的描述符,且这个新的描述符和旧的描述符指向的是同一个文件. 这两种行为有什么区别呢?下面给出的两张文件系统的图形象的解 ...