Python:一个小小程序,如何分析unix是否安装了某个命令

1.  Python在调用unix系统命令之前,需要对系统命令做检测,其中一个检测就是命令是否存在。这里没有引用 which sshpass 的unix命令行模式。哪个更快一些就不得而知了。步骤如下:

 1 #!/usr/bin/env python
 2 
 3 import os
 4 
 5 if __name__=='__main__':
 6     mysshpass=0
 7     for cmdpath in os.environ['PATH'].split(':'):
 8         if os.path.isdir(cmdpath) and 'sshpass' in os.listdir(cmdpath):
 9             mysshpass=1
10     if not mysshpass:
11         print "PLease check sshpass"
12         exit(2)

 

 这里没有用 if 'sshpass' not in os.listdir(cmdpath): 是因为,在有一些bin的目录是没有指定命令的。所以会产生错误判断。

posted @ 2012-04-17 16:28  CCJPP  阅读(2883)  评论(1)    收藏  举报