简体   繁体   中英

Python capturing terminal output

Hey i run into a problem which is how python can capture the output like this command

os.system("apt install apt-transport-https") 

and then if its allready installed to skip it so it dont make a new install ?

Try using os.popen or subprocess :

from subprocess import Popen, PIPE

process = Popen(['apt', 'install', 'apt-transport-https'], stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
print(stdout)

With the output captured in stdout , you can search that string for the results you want.

command = 'apt install apt-transport-https'
r = os.popen(command) #Execute command
info = r.readlines()  #read command output
for line in info:  #handle output line by line
    line = line.strip('\r\n')
    print line

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM