python中执行shell命令行read结果
+++++++++++++++++++++++++++++
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结果的更多相关文章
- python中执行shell命令的几个方法小结(转载)
转载:http://www.jb51.net/article/55327.htm python中执行shell命令的几个方法小结 投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014- ...
- python中执行shell命令的几个方法小结
原文 http://www.jb51.net/article/55327.htm 最近有个需求就是页面上执行shell命令,第一想到的就是os.system, os.system('cat /proc ...
- 「Python」6种python中执行shell命令方法
用Python调用Shell命令有如下几种方式: 第一种: os.system("The command you want"). 这个调用相当直接,且是同步进行的,程序需要阻塞并等 ...
- python中执行shell命令的几个方法
1.os.system() a=os.system("df -hT | awk 'NR==3{print $(NF-1)}'") 该命令会在页面上打印输出结果,但变量不会保留结果, ...
- python中执行shell命令
查看输出结果 import os output = os.popen('cat 6018_gap_5_predict/solusion2/solusion2_0-1.txt | wc -l') pri ...
- Python中执行外部命令
有很多需求需要在Python中执行shell命令.启动子进程,并捕获命令的输出和退出状态码,类似于Java中的Runtime类库. subprocess模块的使用: Python使用最广泛的是标准库的 ...
- vim中执行shell命令小结
vim中执行shell命令,有以下几种形式 1):!command 不退出vim,并执行shell命令command,将命令输出显示在vim的命令区域,不会改变当前编辑的文件的内容 例如:!ls -l ...
- python中执行shell的两种方法总结
这篇文章主要介绍了python中执行shell的两种方法,有两种方法可以在Python中执行SHELL程序,方法一是使用Python的commands包,方法二则是使用subprocess包,这两个包 ...
- 在 Ruby 中执行 Shell 命令的 6 种方法
我们时常会与操作系统交互或在 Ruby 中执行 Shell 命令.Ruby为我们提供了完成该任务的诸多方法. Exec Kernel#exec 通过执行给定的命令来替换当前进程,例如: $ irb & ...
随机推荐
- Python时间戳与时间字符串互相转换实例代码
#设a为字符串import timea = "2011-09-28 10:00:00" #中间过程,一般都需要将字符串转化为时间数组time.strptime(a,'%Y-%m-% ...
- mysql官方下载
https://dev.mysql.com/downloads/file/?id=472899
- 【LeetCode OJ】Swap Nodes in Pairs
题目:Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2 ...
- MyEclipse 10 下在线安装插件
昨天不知道怎么就删除了电脑中的eclipse 我x,还原不回来了. 今天就安装了最新版本的myeclipse10,大家都知道,MyEclipse 中有一个烦人的 Software and Worksp ...
- 基础知识《十二》一篇文章理解Cookie和Session
理解Cookie和Session机制 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定 ...
- 正则-input控制输入
大于0的数字:/^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$/ 这正则看不太懂,先放着 作者:Kevin Yang 使用正则表达式找出不包含特定字符串 ...
- 【PHP】 curl 上传文件 流
在运行过程中, 以下两种方式要看你的PHP 版本 'file' =>'@' .$filePath 'file' =>new CURLFile(realpath($filePath)) 本次 ...
- shell 脚本调试
1.第一行加 -xv #!/bin/bash –xv 2. bash -x shellName 3.如果只想调试其中几行脚本的话可以用 set -x 和 set +x 把要调试的部分包含进来: 比如: ...
- 转载:C/C++关于string.h头文件和string类
学习C语言时,用字符串的函数例如stpcpy().strcat().strcmp()等,要包含头文件string.h 学习C++后,C++有字符串的标准类string,string类也有很多方法,用s ...
- 让人一看就懂的excel相对引用和绝对引用案例解析
http://www.ittribalwo.com/article/2831.html 内容提要:本文的excel相对引用和绝对引用.混合引用的使用方法案例截选自<Excel效率手册 早做完,不 ...