+++++++++++++++++++++++++++++

python执行shell命令
1 os.system  (只有这个方法是边执行边输出,其他方法是最后一次性输出)

可以返回运行shell命令状态,同时会在终端输出运行结果

例如 ipython中运行如下命令,返回运行状态status

os.system('python -V')
os.system('tree')

遇到乱码问题可以采用一次性输出来解决。https://www.cnblogs.com/andy9468/p/8418649.html

或者pycharm的乱码问题:https://www.cnblogs.com/andy9468/p/12766382.html

2 os.popen()

可以返回运行结果

import os

r = os.popen('python -V').read()
print(type(r))
print(r)

  

或者

In [20]: output = os.popen('cat /proc/cpuinfo')

In [21]: lineLen = []

In [22]: for line in output.readlines():
lineLen.append(len(line))
....: In [23]: line
line lineLen In [23]: lineLen
Out[23]:
[14,
25,
...

  

3 commands.getstatusoutput('cat /proc/cpuinfo')

如何同时返回结果和运行状态,commands模块:

import commands
(status, output) = commands.getstatusoutput('cat /proc/cpuinfo') In [25]: status
Out[25]: 0 In [26]: len(output)
Out[26]: 3859

  

4 subprocess.Popen(["ls","-l"], stdout=subprocess.PIPE)

使用模块subprocess

通常项目中经常使用方法为subporcess.Popen, 我们可以在Popen()建立子进程的时候改变标准输入、标准输出和标准错误,并可以利用subprocess.PIPE将多个子进程的输入和输出连接在一起,构成管道(pipe):

import subprocess
child1 = subprocess.Popen("tree",shell=True, stdout=subprocess.PIPE)
out = child1.stdout.read()
print(out.decode('gbk'))

  

import subprocess
child1 = subprocess.Popen("tree /F".split(),shell=True, stdout=subprocess.PIPE)
out = child1.stdout.read()
print(out.decode('gbk'))

  

import subprocess
child1 = subprocess.Popen(['tree','/F'].split(),shell=True, stdout=subprocess.PIPE)
out = child1.stdout.read()
print(out.decode('gbk'))

  

退出进程

size_str = os.popen('adb shell wm size').read()
if not size_str:
  print('请安装 ADB 及驱动并配置环境变量')
  sys.exit()

  

封装好的函数:Python执行shell命令

from subprocess import Popen, PIPE

def run_cmd(cmd):
# Popen call wrapper.return (code, stdout, stderr)
child = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
out, err = child.communicate()
ret = child.wait()
return (ret, out, err) if __name__ == '__main__':
r=run_cmd("dir") print(r[0])
print(r[1].decode("gbk"))
print(r[2])

python中执行shell命令行read结果的更多相关文章

  1. python中执行shell命令的几个方法小结(转载)

    转载:http://www.jb51.net/article/55327.htm python中执行shell命令的几个方法小结 投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014- ...

  2. python中执行shell命令的几个方法小结

    原文 http://www.jb51.net/article/55327.htm 最近有个需求就是页面上执行shell命令,第一想到的就是os.system, os.system('cat /proc ...

  3. 「Python」6种python中执行shell命令方法

    用Python调用Shell命令有如下几种方式: 第一种: os.system("The command you want"). 这个调用相当直接,且是同步进行的,程序需要阻塞并等 ...

  4. python中执行shell命令的几个方法

    1.os.system() a=os.system("df -hT | awk 'NR==3{print $(NF-1)}'") 该命令会在页面上打印输出结果,但变量不会保留结果, ...

  5. python中执行shell命令

    查看输出结果 import os output = os.popen('cat 6018_gap_5_predict/solusion2/solusion2_0-1.txt | wc -l') pri ...

  6. Python中执行外部命令

    有很多需求需要在Python中执行shell命令.启动子进程,并捕获命令的输出和退出状态码,类似于Java中的Runtime类库. subprocess模块的使用: Python使用最广泛的是标准库的 ...

  7. vim中执行shell命令小结

    vim中执行shell命令,有以下几种形式 1):!command 不退出vim,并执行shell命令command,将命令输出显示在vim的命令区域,不会改变当前编辑的文件的内容 例如:!ls -l ...

  8. python中执行shell的两种方法总结

    这篇文章主要介绍了python中执行shell的两种方法,有两种方法可以在Python中执行SHELL程序,方法一是使用Python的commands包,方法二则是使用subprocess包,这两个包 ...

  9. 在 Ruby 中执行 Shell 命令的 6 种方法

    我们时常会与操作系统交互或在 Ruby 中执行 Shell 命令.Ruby为我们提供了完成该任务的诸多方法. Exec Kernel#exec 通过执行给定的命令来替换当前进程,例如: $ irb & ...

随机推荐

  1. N76E003之ISP

    Flash存储器支持硬件编程和应用编程(IAP).如果产品在研发阶段或产品需要更新软固件时,硬件编程就显得不太方便,采用在系统编程(ISP)方式,可使这一过程变得方便.执行ISP不需要将控制器从系统板 ...

  2. button按钮不能点击鼠标形状css 代码,禁用button按钮时鼠标形状

    cursor:not-allowed;

  3. MySql数据库设计表添加字段

    当要添加的字段属于整型,需要设置默认值 或者: alter table fp_user_base add hasPwd tinyint(4) not null default 0;

  4. Oralce分析函数

    1 列传行  listagg(city,',')  within GROUP (order by city)    over (partition by nation) rank with temp ...

  5. java上传并压缩图片(等比例压缩或者原尺寸压缩)

    本文转载自http://www.voidcn.com/article/p-npjxrbxr-kd.html 先看效果: 原图:1.33M 处理后:27.4kb 关键代码; package codeGe ...

  6. vue经验 - 那些自己给自己挖的深坑

    深坑场景:vue-异步请求数据,数据还没回来,页面却如饥似渴的准备好了的尴尬场景:问题原因和解决如下: 1.先说vuex中的store,一开始我为了偷懒是这么设置的,如下图: 然后我到了组件中直接这么 ...

  7. libevent安装方法

    安装FastDFS之前,先安装libevent工具包,记录一下安装过程 1.检查:ls -al /usr/lib | grep libevent 查看是否已安装,如果已安装且版本低于1.3,则先通过: ...

  8. Apache Server Status详解

    Apache的日志如果靠分析日志或者查看服务器进程来监视Apache运行状态的话,比较繁冗.不过在Apache 1.3.2及以后的版本中就自带一个查看Apache状态的功能模块server-statu ...

  9. CF 445A DZY Loves Chessboard

    A. DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input stand ...

  10. JavaAgent入门

    JavaAgent 是JDK 1.5 以后引入的,也可以叫做Java代理. JavaAgent 是运行在 main方法之前的拦截器,它内定的方法名叫 premain ,也就是说先执行 premain ...