如何保存来自“sudo dpkg -l"的数据在 Ubuntu 终端中使用 python [英] How to save the data coming from "sudo dpkg -l" in Ubuntu terminal by using python

查看:30
本文介绍了如何保存来自“sudo dpkg -l"的数据在 Ubuntu 终端中使用 python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用python在Ubuntu终端中保存来自sudo dpkg -l"的数据,我尝试这样做,但它不起作用

How to save the data coming from "sudo dpkg -l" in Ubuntu terminal by using python, I tried to do it this way, but it doesn't work

import os
f = open('/tmp/dpgk.txt','w')
f.write(os.system('sudo dpkg -l'))

推荐答案

使用 subprocess.check_output() 捕获另一个进程的输出:

Use subprocess.check_output() to capture the output of another process:

import subprocess

output = subprocess.check_output(['sudo', 'dpkg', '-l'])

os.system() 只返回另一个进程的退出状态.上面的例子假设 sudo 不会提示输入密码.

os.system() only returns the exit status of another process. The above example presumes that sudo is not going to prompt for a password.

这篇关于如何保存来自“sudo dpkg -l"的数据在 Ubuntu 终端中使用 python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆