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. python之histogram

    histogram A histogram is an accurate representation of the distribution of numerical data. It is an ...

  2. vs2010 MSDN文档安装方法

    vs2010的MSDN是不能独立安装,必须安装VS2010后才能安装. 安装方法: 1.vs2010的ISO光盘文件中,里面会有个ProductDocumentation文件夹,其实这个就是安装MSD ...

  3. 使用code::blocks搭建objective-c的IDE开发环境 支持 @interface

    网上有许多的关于 <使用code::blocks搭建objective-c的IDE开发环境>的文章. 大多是写了一个Helloworld 就结束了,今天试了试 添加了一个 @interfa ...

  4. openresty安装

    cd /usr/local/mkdir datacd datawget http://openresty.org/download/openresty-1.9.15.1.tar.gz cd /usr/ ...

  5. Unity3D避免代码被反编译

    1.Unity编译后最终会将代码编译在dll里面,无论是ios还是Android解开包以后都可以拿到dll,路径在Data/Managed/Assembly-CSharp.dll 2.IOS其实不用做 ...

  6. 【Java】之static静态方法与非static静态方法区别

    1.A.class:没有static public class A { public String getText(){ } B.class调用A的方法时 public class B { publi ...

  7. easyui.dialog.js

    (function ($) { var $parent = parent.$; //获取弹出窗口数据集合 function getDialogs() { var dialogs = $parent(& ...

  8. sklearn解决分类问题(KNN,线性判别函数,二次判别函数,KMeans,MLE,人工神经网络)

    代码:*******************加密中**************************************

  9. ARGOX 力象 OS-214Plus 条码打印机 B/S 打印

    官网demo下载地址: http://www.argox.com.cn/servicedev/5/c 页面中嵌入activeX控件: <object id="ArgoxPrinter& ...

  10. poj 2503 Babelfish(字典树或着STL)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 35828   Accepted: 15320 Descr ...