python初步学习-python模块之 commands

commands

通过 os.popen() 执行 shell 命令,返回两个对象,一个是 状态码(Int)、另一个为命令输出(str)

commands.getoutput(cmd)

返回命令执行输出,包括正确输出或者错误输出。

commands.getstatus(file)

查看文件或者目录的状态,类似

ls -ld file

例如:

import commands
print commands.getstatus('/tmp/test')   #/tmp/test是一个目录
print commands.getstatus('/tmp/test/file.txt')    #file.txt是一个文件

#返回结果:
>>> import commands
>>> print commands.getstatus('/tmp/test')
drwxr-xr-x 2 root root 4096 Oct 27 03:41 /tmp/test


>>> print commands.getstatus('/tmp/test/file.txt')
-rw-r--r-- 1 root root 857 Oct 27 03:41 /tmp/test/file.txt

与 ls -ld 效果一样

commands.getstatusoutput(cmd)

返回(status,output)

  • status 为执行命令的返回码
  • output 为执行命令的输出
posted @ 2017-11-11 01:40  PING1  阅读(576)  评论(0编辑  收藏  举报