parsing a file [newbie]

Grant Edwards grante at visi.com
Wed Jan 17 17:45:14 EST 2001


In article <t%o96.174$eI2.99668 at ruti.visi.com>, Grant Edwards wrote:

>>ONe thing I don't understand. When I run os.popen('df -h'),
>>this returns a pointer to an already open file if I am not
>>mistaken.
[...]
>>Now, to apply what you gave me to this scenario, how would I go about it.
>
>You call the read() or readlines() method of that file object.
>Something like this:
>
>import os
>import string
>
>x = os.popen("mount").readlines()
>y = []
>for line in x:
>    words = string.split(line)
>    if len(words) >= 3:
>        y.append(words[2])

I should point out that were I cool enough to have Python 2.0
installed, I could have used a list comprehension instead of a
for loop.  The for loop is probably a bit more intuitive for a
beginner.

-- 
Grant Edwards                   grante             Yow!  I want a VEGETARIAN
                                  at               BURRITO to go... with
                               visi.com            EXTRA MSG!!



More information about the Python-list mailing list