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

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. php技能评测

    以下摘抄自:https://www.viphper.com/?p=1236 公司出了一些自我评测的PHP题目,现将题目和答案记录于此,以方便记忆. 1. 魔术函数有哪些,分别在什么时候调用?__con ...

  2. linux C 调用shell程序执行

    #include<stdio.h> #include <unistd.h> #include <sys/types.h> #include <stdlib.h ...

  3. VC下遍历文件夹中的所有文件的几种方法

    一.使用::FindFirstFile和::FindNextFile方法 #include "StdAfx.h" #include <windows.h> #inclu ...

  4. 【NET多线程】C#多线程异步请求多个url地址

    异步测试代码 System.Diagnostics.Debug.Print("start"); new Thread(new ThreadStart(new Action(() = ...

  5. 【Linux】`ImportError: No module named '_tkinter'

    在centos7 系统下,导入matplotlib时,出现ImportError: No module named '_tkinter'的错误 首先使用以下命令查看模块是否存在 yum list in ...

  6. Why is IMAP better than POP?

    https://www.fastmail.com/help/technical/imapvspop.html POP is a very simple protocol that only allow ...

  7. spring整合websocket通信

    1. maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...

  8. 使用COSBench工具对ceph s3接口进行压力测试

    一.COSBench安装 COSBench是Intel团队基于java开发,对云存储的测试工具,全称是Cloud object Storage Bench 吐槽下,貌似这套工具是intel上海团队开发 ...

  9. Windows Server 2008 R2之五操作主控的管理

    一.概述 操作主控(FSMO)也称作操作主机(OM),它是指在AD中一个或多个特殊的DC,用来执行某些特殊的功能(资源标识符SID分配.架构修改.PDC选择等). 1.操作主控的分类 基于森林的操作主 ...

  10. python开发环境搭建(python3.3.2+wing IDE4.1)

    1.下载python http://www.wingide.com/downloads下载最新版python 2.下载Wing IDE http://wingware.com/downloads/wi ...