question re file as sequence

Paul Rubin http
Sun Jul 11 01:50:26 CEST 2004


I have a file with contents like:

   Vegetable:
   spinach
   Fruit:
   banana
   Flower:
   Daisy
   Fruit:
   pear

I want to print out the names of all the fruits.  Is the following valid?

   f = open(filename) 
   for line in f:
      if line == 'Fruit:\n':
         print f.readline()

The issue here is that the sequence has been mutated inside the loop.

If the above isn't good, anyone have a favorite alternative to doing
it like that?

Thanks.



More information about the Python-list mailing list