a popen question. Please help

Tim Chase python.list at tim.thechases.com
Sun Aug 30 13:43:19 CEST 2009

> texts = os.popen('top').readlines()
> print texts
> It calls the command line "top" and will print out some texts.
> But first I have to press the keyboard "q" to quit the subprocess "top", then the texts 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 and print out the texts.

Well as a workaround, my version of top (on Debian) supports a 
"-n" parameter so you can tell it how many iterations you want it 
to perform before quitting.  So you should be able to just use

   os.popen('top -n1').readlines()


More information about the Python-list mailing list