Python——调用shell命令的三种方法
1.用os.system(cmd) 不过取不了返回值
2.用os.popen(cmd) 要得到命令的输出内容,只需再调用下read()或readlines()等 如a=os.popen(cmd).read()
3.用 commands 模块。其实也是对popen的封装。此模块主要有如下方法
- commands.getstatusoutput(cmd) 返回(status, output).
- commands.getoutput(cmd) 只返回输出结果
- commands.getstatus(file) 返回ls -ld file的执行结果字符串,调用了getoutput,不建议使用此方法.
- 如
>>> 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命令的三种方法的更多相关文章
- awk调用shell命令的两种方法:system与print
from:http://www.oklinux.cn/html/developer/shell/20070626/31550.htmlawk中使用的shell命令,有2种方法: 一.使用所以syste ...
- python 调取 shell 命令的几种方法
os.system()无法获得到输出和返回值 os.popen()output = os.popen('cat /proc/cpuinfo')print output.read()返回的是 file ...
- python 调用shell命令三种方法
#!/usr/bin/python是告诉操作系统执行这个脚本的时候,调用/usr/bin下的python解释器: #!/usr/bin/env python这种用法是为了防止操作系统用户没有将pyth ...
- python 调用 shell 命令方法
python调用shell命令方法 1.os.system(cmd) 缺点:不能获取返回值 2.os.popen(cmd) 要得到命令的输出内容,只需再调用下read()或readlines()等 ...
- 用Python调用Shell命令
Python经常被称作“胶水语言”,因为它能够轻易地操作其他程序,轻易地包装使用其他语言编写的库,也当然可以用Python调用Shell命令. 用Python调用Shell命令有如下几种方式: 第一种 ...
- 调用sed命令的三种方式
调用sed命令的三种方式 调用sed有三种方式,一种为Shell命令行方式,另外两种是将sed命令写入脚本文件,然后执行该脚本文件. 三种方式的命令格式归纳如下: 一.在Shell命令行输入命令调用s ...
- python 调用 shell 命令
记录 python 调用 shell 命令的方法 加载 os 模块, 使用 os 类 import os; os.system("ls /");
- python执行linux命令的两种方法
python执行linux命令有两种方法: 在此以Linux常用的ls命令为例: 方法一:使用os模块 1 2 3 shell# python >> import os >> ...
- python实现XML解析的三种方法
python实现XML解析的三种方法 三种方法:一是xml.dom.*模块,它是W3C DOM API的实现,若需要处理DOM API则该模块很适合:二是xml.sax.*模块,它是SAX API的实 ...
随机推荐
- Mysql缺少可执行的命令
MySQL问题解决:-bash:mysql:command not found 问题: [root@linux115 /]# mysql -uroot -p -bash: m ...
- Vue 不使用Vuex的情况下进行状态管理
在封装自己的Vue ui库的时候,往往要封装一些比较复杂的组件,比如说table,form之类.这些组件由于功能繁杂,还涉及到子组件嵌套及通信,如果没有一套状态管理方案的话很容易导致代码难以阅读.难以 ...
- [scrapy] spider object has no attribute '_rules'
这是因为__init__方法没有继承父类 解决办法: # -*- coding:utf-8 -*- from selenium import webdriver from scrapy.contrib ...
- 百度之星资格赛 2016 Problem 1001
本文链接http://www.cnblogs.com/Ash-ly/p/5494618.html 题意: 度熊手上有一本字典存储了大量的单词,有一次,他把所有单词组成了一个很长很长的字符串.现在麻烦来 ...
- (寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest
layout: post title: (寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest author: "luow ...
- Codeforces Round #124 (Div. 1) C. Paint Tree(极角排序)
C. Paint Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Express处理GET/POST请求(POST请求包含文件)
Express处理GET/POST请求(POST请求包含文件) GET 使用简洁的pug模板引擎,写一个表单,提交方法是GET 前端页面代码 enctype,默认是application/x-www- ...
- mdadm Raid5 /dev/md0 lost a disk and recovery from another machine
centos -- how to add a new disk into a mdadm raid5 /dev/md0 which lost a /dev/sdc1 disk and revoery ...
- java 中==符号的坑
在某技术群看到这样的一个面试题目: 这是一个4年经验的java 从业者的答案. 你的答案是什么呢? 正确的答案是true. 为什么? 其实当使用String a="a"+" ...
- [Codeforces 23D] Tetragon
Brief Intro: 给3条相同长度的边的中点,问是否存在一个严格凸四边形 Algorithm: 明显只要求出一个点就能利用对称性算出其他点的坐标 设中点K,L,M分别在边AB,BC,CD上,易知 ...