command使用方法
来源:互联网 发布:阿里云备案导入 编辑:程序博客网 时间:2024/06/05 16:17
python -> Commands模块 -> 使用方法
要获得shell命令的输出只需要`cmd`就可以了,
需要得到命令执行的状态则需要判断$?的值, 在Python中有一个模块commands也很容易做到以上的效果.
看一下三个函数:
1). commands.getstatusoutput(cmd)
用os.popen()执行命令cmd, 然后返回两个元素的元组(status, result). cmd执行的方式是{ cmd ; } 2>&1, 这样返回结果里面就会包含标准输出和标准错误.
2). commands.getoutput(cmd)
只返回执行的结果, 忽略返回值.
3). commands.getstatus(file)
返回ls -ld file执行的结果.
看一下这些函数使用的例子:
>>> 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'
链接出处:http://hi.baidu.com/liuhelishuang/item/ab6f7c36d55e81f52784f4a8
- command使用方法
- DevComponents.DotNetBar.Command使用方法
- Visual Studio build event command line 使用方法
- Visual Studio build event command line 使用方法
- Command
- Command
- Command
- command
- Command
- Command
- command
- command!!!
- command
- command
- Command
- Command
- Command
- Command
- opencv_笔记_core_basic structures_1117
- C++继承:公有,私有,保护
- UVA - 11795 Mega Man's Mission
- JSP翻页功能代码
- 高仿QQ电脑管家8 界面
- command使用方法
- press TAB to show Post screen,DEL to enter SETUP
- Perl获取web表单键值对的子程序
- 该天为该年第几天
- Implementing Runtime Object Messaging
- UVA 10201 Adventures in Moving - Part IV 车站加油 dp
- Linux系统操作技巧集萃
- CMD的常用命令
- 用busybox作简单的文件系统(一)