是否有一种优雅且更像Python的方法来检查Debian上是否安装了软件包?
在bash脚本中,我会这样做:
dpkg -s packagename | grep Status
Run Code Online (Sandbox Code Playgroud)
建议在Python脚本中执行相同的操作?
谢谢,
max*_*amo 19
这是一种pythonic方式:
import apt
cache = apt.Cache()
if cache['package-name'].is_installed:
print "YES it's installed"
else:
print "NO it's NOT installed"
Run Code Online (Sandbox Code Playgroud)
bash 示例的一个稍微好一点的,希望是惯用的版本:
import os, subprocess
devnull = open(os.devnull,"w")
retval = subprocess.call(["dpkg","-s","coreutils"],stdout=devnull,stderr=subprocess.STDOUT)
devnull.close()
if retval != 0:
print "Package coreutils not installed."
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11051 次 |
| 最近记录: |