任务清单输出 [英] Tasklist output

查看:95
本文介绍了任务清单输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对python还是很陌生,但是我找不到我认为应该是相对简单的问题的答案.

I am pretty new to python, but I am unable to find an answer to what I am thinking should be a relatively simple issue.

我正在尝试使用tasklist,我想知道如何使用它的输出(例如将其设置为变量,数组等).

I am trying to utilize tasklist, and I am wondering what I can do with the output of it (like set it to a variable, an array, things like that).

我正在使用Python 3.3,在查找有关3.3的文档时遇到了一些麻烦.

I am using Python 3.3, and I have had some trouble finding documentation on 3.3.

代码相对简单:

import os
os.system("tasklist")
input()

这将打印任务列表,但是我很难将数据从该打印结果转换为变量.我假设这与Python无关,而与Tasklist无关.

This prints the tasklist, but I have had trouble getting data from that print into variables. I am assuming it's something minor to do with Python, and not to do with tasklist.

最终,我正在寻找一个任务列表条目的矩阵,这样我就可以搜索进程并获取相应的数据.

Ultimately I am looking to make a matrix of the tasklist entries, that way I can search for a process, and grab the corresponding data.

推荐答案

subprocess.check_output是最简单的:

(请注意,我在这里没有使用过 ps 您正在谈论的tasklist命令-尽管如此,但对于窗户系统还是有参考... )

(Note I've used ps here, as I'm not experienced with the tasklist command you're talking about - there's reference to it for window systems though...)

>>> import subprocess
>>> res = subprocess.check_output(['ps'])
>>> res
'  PID TTY          TIME CMD\n 1749 ?        00:00:00 gnome-keyring-d\n 1760 ?        00:00:00 gnome-session\n 1797 ?        00:00:00 ssh-agent\n 1800 ?        00:00:00 dbus-launch\n 1801 ?        00:00:04 dbus-daemon\n 1814 ?        00:00:09 gnome-settings-\n 1819 ?        00:00:00 gvfsd\n 1821 ?        00:00:00 gvfs-fuse-daemo\n 1829 ?        00:11:51 compiz\n 1832 ?        00:00:00 gconfd-2\n 1838 ?        00:00:29 syndaemon\n 1843 ?        00:34:44 pulseaudio\n 1847 ?        00:00:00 gconf-helper\n 1849 ?        00:00:00 gvfsd-metadata\n 1851 ?        00:00:00 bluetooth-apple\n 1852 ?        00:00:04 nautilus\n 1853 ?        00:00:01 nm-applet\n 1855 ?        00:00:00 polkit-gnome-au\n 1856 ?        00:00:00 gnome-fallback-\n 1873'

然后您必须在res上执行某些操作,这样它才可用...

Then you have to do something on res so it's usable...

这篇关于任务清单输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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