博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
commands 模块 分类: python 小练习 ...
阅读量:4330 次
发布时间:2019-06-06

本文共 888 字,大约阅读时间需要 2 分钟。

commands 模块包含一些用于执行外部命令的函数. Example 3-7展示了这个模块.

3.5.0.1. Example 3-7. 使用 commands 模块

import commands
stat, output = commands.getstatusoutput("ls -lR")
print "status", "=>", stat
print "output", "=>", len(output), "bytes"

用 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')

>>> print commands.getoutput('ls')

little.py
myyield.py
new
personel.py
personel.pyc

>>> commands.getstatus("little.py")
'-rw-r--r-- 1 root root 468 01-10 17:53 little.py'

转载于:https://www.cnblogs.com/think1988/p/4627966.html

你可能感兴趣的文章
jinja2.exceptions.TemplateNotFound: home/index.html
查看>>
HTML的盒子模型(border和css的3种布局)
查看>>
codesmith注册
查看>>
【Lintcode】 maximum gap
查看>>
IOS - Passbook
查看>>
js 框架
查看>>
LINUX PID 1 和 SYSTEMD
查看>>
centos6挂载U盘
查看>>
《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #11cpuset
查看>>
NVMe on RHEL7
查看>>
将jetty嵌入到应用中的简单案例
查看>>
cookie的操作
查看>>
test
查看>>
linux read 命令
查看>>
转:Linux fork与vfork的深入分析
查看>>
Three.js构造一个简单的房间
查看>>
UFLDL深度学习笔记 (二)SoftMax 回归(矩阵化推导)
查看>>
Cobbler自动化部署
查看>>
Java— 方法
查看>>
[Android学习系列10]关于Task,Activity,BackStack的一些事
查看>>