python中执行shell命令的几个方法小结
原文 http://www.jb51.net/article/55327.htm
最近有个需求就是页面上执行shell命令,第一想到的就是os.system,
os.system('cat /proc/cpuinfo')
但是发现页面上打印的命令执行结果 0或者1,当然不满足需求了。
尝试第二种方案 os.popen()
output = os.popen('cat /proc/cpuinfo')
print output.read()
通过 os.popen() 返回的是 file read 的对象,对其进行读取 read() 的操作可以看到执行的输出。但是无法读取程序执行的返回值)
尝试第三种方案 commands.getstatusoutput() 一个方法就可以获得到返回值和输出,非常好用。
(status, output) = commands.getstatusoutput('cat /proc/cpuinfo')
print status, output
Python Document 中给的一个例子,
>>> import commands
>>> commands.getstatusoutput('ls /bin/ls')
(0, '/bin/ls')
>>> commands.getstatusoutput('cat /bin/junk')
(256, 'cat: /bin/junk: No such file or directory')
>>> commands.getstatusoutput('/bin/junk')
(256, 'sh: /bin/junk: not found')
>>> commands.getoutput('ls /bin/ls')
'/bin/ls'
>>> commands.getstatus('/bin/ls')
'-rwxr-xr-x 1 root 13352 Oct 14 1994 /bin/ls'
最后页面上还可以根据返回值来显示命令执行结果。
python中执行shell命令的几个方法小结的更多相关文章
- python中执行shell命令的几个方法小结(转载)
转载:http://www.jb51.net/article/55327.htm python中执行shell命令的几个方法小结 投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014- ...
- python中执行shell命令的几个方法
1.os.system() a=os.system("df -hT | awk 'NR==3{print $(NF-1)}'") 该命令会在页面上打印输出结果,但变量不会保留结果, ...
- 在 Ruby 中执行 Shell 命令的 6 种方法
我们时常会与操作系统交互或在 Ruby 中执行 Shell 命令.Ruby为我们提供了完成该任务的诸多方法. Exec Kernel#exec 通过执行给定的命令来替换当前进程,例如: $ irb & ...
- python中执行shell命令行read结果
+++++++++++++++++++++++++++++ python执行shell命令1 os.system 可以返回运行shell命令状态,同时会在终端输出运行结果 例如 ipython中运行如 ...
- 「Python」6种python中执行shell命令方法
用Python调用Shell命令有如下几种方式: 第一种: os.system("The command you want"). 这个调用相当直接,且是同步进行的,程序需要阻塞并等 ...
- 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使用最广泛的是标准库的 ...
- python中执行shell的两种方法总结
这篇文章主要介绍了python中执行shell的两种方法,有两种方法可以在Python中执行SHELL程序,方法一是使用Python的commands包,方法二则是使用subprocess包,这两个包 ...
- vim中执行shell命令小结
vim中执行shell命令,有以下几种形式 1):!command 不退出vim,并执行shell命令command,将命令输出显示在vim的命令区域,不会改变当前编辑的文件的内容 例如:!ls -l ...
随机推荐
- Mayor's posters(线段树+离散化)
这道题最关键的点就在离散化吧. 假如有三张海报[1, 10] [10, 13][15, 20] 仅仅三个区间就得占用到20了. 但是离散化后就可以是[1, 2] [2, 3] [4, 5] n到1e ...
- 提交已经注入文件的表单给后台上传图片 使用ajaxsubmit
- Uncaught SyntaxError: Unexpected token export
开发过程中遇到这个错误,虽然不影响使用,但是每次浏览器控制台都会有错误输出,看起来十分不舒服,故翻阅资料发现是因为浏览器虽然支持了es6,但是不支持es6的Module直接使用,需要在script标签 ...
- python模块psutil的使用
介绍 psutil是一个跨平台库(http://code.google.com/p/psutil/),能够轻松实现获取系统运行的进程和系统利用率(包括CPU.内存.磁盘.网络等)信息.它主要应用于系统 ...
- IDEA Failed to prepare an update: Temp directory inside installation
具体错误: Connection Error Failed to prepare an update: Temp directory inside installation: F:\IDEA_Tool ...
- springboot 简单搭建
springboot的入门请参考:https://blog.csdn.net/hanjun0612/article/details/81538449 这里就简单看下搭建: 一,看一下项目结构: 创建一 ...
- 洛谷P1118数字三角形题解
题目 这个题我们乍一看会有些熟悉.觉得是可以用DP来做的那个题.但是打眼一看,就会发现不对了.因为那个题是顺推而这个题则是逆推. 这样的话可怎么办呢. 我们可以在草稿纸上推一下,我们随便写个数n. 再 ...
- emwin之求解窗口坐标及大小的一种方法
@2019-01-26 [小记] 使用函数 WM_GetWindowRectEx(hItem, &Rect),坐标就存储在对象 Rect 中,可用于一些默认创建的窗口
- su命令详解
-----------------------------------------------------------------su 权限设置[root@localhost ~]# vim /etc ...
- Bomb HDU - 5934 (Tarjan)
#include<map> #include<set> #include<ctime> #include<cmath> #include<stac ...