output of shell command

Grant Edwards grante at visi.com
Mon Feb 26 17:11:30 EST 2001


In article <n9hl9tc8v4d3bg7vkoq29k4v6oppq6lon3 at 4ax.com>, Sheila King wrote:

>:Thanks guys for the help, one note is that os.popen().readlines() of
>:course will return a list of strings - lines of output, whereas
>:commands.getoutput() will return a single string. So both will come in
>:handy.
>
>You can return a single string by turning the readlines() into read()
>like this:
>
>os.popen().read()
>
>Or, do this (silly):
>
>"\n".join(os.popen("dir").readlines()))

[I swear, no matter how many times I see that syntax, it looks
goofy.]

Likewise, if you want a list of strings from commands.getoutput()
you can use the split() method:

commands.getoutput().split('\n')

-- 
Grant Edwards                   grante             Yow!  Now I'm concentrating
                                  at               on a specific tank battle
                               visi.com            toward the end of World
                                                   War II!



More information about the Python-list mailing list