Check if a Debian package is installed from Python

后端 未结 7 1523
一生所求
一生所求 2020-12-31 03:20

Is there an elegant and more Python-like way to check if a package is installed on Debian?

In a bash script, I\'d do:

dpkg -s packagename | grep Stat         


        
7条回答
  •  既然无缘
    2020-12-31 04:04

    A slightly nicer, hopefully idiomatic version of your bash example:

    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."
    

提交回复
热议问题