a popen command line question
Nitebirdz
nitebirdz at sacredchaos.com
Mon Aug 31 10:40:50 EDT 2009
On Sat, Aug 29, 2009 at 01:13:12PM -0700, Joni Lee wrote:
> Hi all,
>
> I write a small script
>
> status = os.popen('top').readlines()
> print status
>
> It calls the command line "top" and will print out the status.
> But I have to press the keyboard "q" to quit "top", then the status
> will be printed, otherwise it just stands by with blank.
>
> Question is. Do you know how to give "q" into my python script so that
> "top" is automatically quit immediately or maybe after 1s (for
> gathering information)
>
Why not run the actual 'top' command in batch mode with only one
iteration? It's in the man page. I'm referring to this:
status = os.popen('top -b -n 1').readlines()
Also, the following approach seems to display a nicer output:
-------------------------------------------------
#!/usr/bin/env python
import os
status=os.popen('top -b -n 1')
for i in status.readlines():
print i
-------------------------------------------------
Or how about the following?
-------------------------------------------------
#!/usr/bin/env python
import commands
status=commands.getoutput('top -b -n 1')
print status
-------------------------------------------------
Mind you, I'm just suggesting different approaches. I'm new to Python
and don't necessarily know what I'm doing. Other more experienced
people may be able to suggest something better.
More information about the Python-list
mailing list