Python执行系统命令的方法 os.system(),os.popen(),commands
2012-05-22 09:03
901 查看
最开始的时候用 Python 学会了 os.system() 这个方法是很多比如 C,Perl 相似的。
os.system('cat /proc/cpuinfo') |
output = os.popen('cat /proc/cpuinfo') print output.read() |
— Utilities for running commands。
这样通过 commands.getstatusoutput() 一个方法就可以获得到返回值和输出,非常好用。
(status, output) = commands.getstatusoutput('cat /proc/cpuinfo') print status, output |
>>> 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执行系统命令的方法 os.system(),os.popen(),commands
- Python执行系统命令的方法 os.system(),os.popen(),commands
- Python执行系统命令的方法 os.system(),os.popen(),commands - renwofei423的个人空间 - 开源中国社区
- Python执行系统命令的方法 os.system(),os.popen(),commands
- [转] Python执行系统命令的方法 os.system(),os.popen(),commands
- Python执行系统命令的方法 os.system(),os.popen(),commands
- Python执行系统命令的方法 os.system(),os.popen(),commands
- Python执行系统命令的方法 os.system(),os.popen(),commands
- Python执行系统命令的方法 os.system(),os.popen(),commands
- Python执行系统命令的方法 os.system(),os.popen(),commands
- Python执行系统命令的方法 os.system(),os.popen(),commands
- Python执行系统命令的方法 os.system(),os.popen(),commands
- Python执行系统命令的方法 os.system(),os.popen(),commands
- [转]Python执行系统命令的方法 os.system(),os.popen(),commands
- Python执行系统命令的方法 os.system(),os.popen(),commands
- Python执行系统命令的方法 os.system(),os.popen(),commands
- Python执行系统命令的方法 os.system(),os.popen(),commands
- Python执行系统命令的方法 os.system(),os.popen(),commands
- Python执行系统命令的方法 os.system(),os.popen(),commands
- Python执行系统命令的方法 os.system(),os.popen(),commands