python2.7执行Linux系统命令
2014-04-29 14:44
567 查看
http://blog.chinaunix.net/uid-16844903-id-57786.html
1·os.system
举例:
In
[34]:
import os
In [35]: os.system('ls
/home')
cacti nagios oracle roottest1 roottest2 test6 test7
Out[35]: 0
优点:直接显示命令的输出和返回值
2·os.popen
举例:
In
[36]: os.popen('ls /home')
Out[36]:
<open file 'ls /home', mode
'r' at 0xeacf60>
In [37]: os.popen('ls
/home').read()
Out[37]:
'cacti\nnagios\noracle\nroottest1\nroottest2\ntest6\ntest7\n'
In [38]: file_tmp
= os.popen('ls /home')
In [39]:
for i in file_tmp:
....:
print i
....:
....:
cacti
nagios
oracle
roottest1
roottest2
test6
test7
优点:可以将命令的输出保存在变量中,然后以多种方式展示出来
3·commands.getstatusoutput
举例:
In
[40]:
import commands
In [41]:
(status, output)
= commands.getstatusoutput('ls /home')
In [42]:
print status
0
In [43]:
print output
cacti
nagios
oracle
roottest1
roottest2
test6
test7
In [44]: commands.getstatusoutput('ls
/home')
Out[44]:
(0,
'cacti\nnagios\noracle\nroottest1\nroottest2\ntest6\ntest7')
优点:命令输出和返回值分开显示
help资料
FUNCTIONS
getoutput(cmd)
Return output (stdout or stderr) of executing cmd in a shell.
getstatus(file)
Return output of "ls -ld <file>" in a string.
getstatusoutput(cmd)
Return (status, output) of executing cmd in a shell.
In [45]: commands.getoutput('ls /home')
Out[45]: 'cacti\nnagios\noracle\nroottest1\nroottest2\ntest6\ntest7'
In [48]: commands.getstatus('/bin/ls')
Out[48]: '-rwxr-xr-x 1 root root 91240 2010-03-01 /bin/ls'
1·os.system
举例:
In
[34]:
import os
In [35]: os.system('ls
/home')
cacti nagios oracle roottest1 roottest2 test6 test7
Out[35]: 0
优点:直接显示命令的输出和返回值
2·os.popen
举例:
In
[36]: os.popen('ls /home')
Out[36]:
<open file 'ls /home', mode
'r' at 0xeacf60>
In [37]: os.popen('ls
/home').read()
Out[37]:
'cacti\nnagios\noracle\nroottest1\nroottest2\ntest6\ntest7\n'
In [38]: file_tmp
= os.popen('ls /home')
In [39]:
for i in file_tmp:
....:
print i
....:
....:
cacti
nagios
oracle
roottest1
roottest2
test6
test7
优点:可以将命令的输出保存在变量中,然后以多种方式展示出来
3·commands.getstatusoutput
举例:
In
[40]:
import commands
In [41]:
(status, output)
= commands.getstatusoutput('ls /home')
In [42]:
print status
0
In [43]:
print output
cacti
nagios
oracle
roottest1
roottest2
test6
test7
In [44]: commands.getstatusoutput('ls
/home')
Out[44]:
(0,
'cacti\nnagios\noracle\nroottest1\nroottest2\ntest6\ntest7')
优点:命令输出和返回值分开显示
help资料
FUNCTIONS
getoutput(cmd)
Return output (stdout or stderr) of executing cmd in a shell.
getstatus(file)
Return output of "ls -ld <file>" in a string.
getstatusoutput(cmd)
Return (status, output) of executing cmd in a shell.
In [45]: commands.getoutput('ls /home')
Out[45]: 'cacti\nnagios\noracle\nroottest1\nroottest2\ntest6\ntest7'
In [48]: commands.getstatus('/bin/ls')
Out[48]: '-rwxr-xr-x 1 root root 91240 2010-03-01 /bin/ls'
相关文章推荐
- Linux 内核编译
- CentOS安装之后的系统安全配置
- 关于mysql安装后更改数据库路径方法-linux环境
- CentOS 安装 PostgreSQL 9.3
- linux上打包程序tar && 压缩程序 && 源代码安装软件 --简介
- CentOS 6.4 安装搭建 Scrapy 0.22 环境
- linux基础知识
- Linux中SCSI总线的磁盘扫描与盘符管理
- 利用U盘与163的yum源镜像网站安装Centos6.5 x86_64系统 推荐
- 查看Linux登陆日志
- ubuntu与centos安装软件的不同点总结
- linux grep命令
- linux下的find文件查找命令与grep文件内容查找命令
- CentOS通过DNSpod实现动态域名
- Linux串口——Struct termios结构体
- Linux下高并发socket最大连接数所受的各种限制 .
- Linux系统下如何查看及修改文件读写权限
- linux 使用debugfs查看gpio占用情况
- Linux内核之基数树
- 嵌入式 Linux下进行广播包交互示例