xreadlines() being used with file.tell() and file.seek()

Pernell Williams pernell.h.williams.nospam at intel.com
Mon Mar 15 14:22:33 EST 2004


Hi all:



I am new to Python, and this is my first post (and it won't be my last!), so
HELLO EVERYONE!! I am attempting to use "xreadlines", an outer loop and an
inner loop in conjunction with "file.tell() and file.seek() in order to
navigate through a file in order to print specific lines (for example, every
5th line). Allow me to illustrate by example:



I have a file like this:



1:one

2:two

3:three

4:four

5:five

6:six

7:seven

8:eight

9:nine

10:ten

11:eleven



And I would like to use an outer and inner loop with the xreadlines()
command in order to provide this output:



the line I would like to print is 1

the line I would like to print is 6

the line I would like to print is 11



Now, I have code using "readline() " in both the inner and outer loop. This
code gives me the output that I am looking for:



------------------------------------------------------------------------

#!/usr/bin/env python



#opening the file for reading

str = 'myFile'

file = open(str, 'r')



#outer loop using file.readline



while 1:

  line = file.readline()

  if line == '':

    break

  data = line.rstrip().split(':')

  print "the line I would like to print is", data [0]



  #inner loop is reading contents of the file,

  #and aftr 5 iterations,

  # going back to  outer loop



  count = 0

  while 1:



    position = file.tell()

    count = count +1

    data = file.readline().strip().split(":")

    if count == 5:

      file.seek(position)

      break



  # end of inner while loop

#end of outer while loop



file.close()



--------------------------------------------------------------------

However, I have code using xreadlines() that does NOT work:





--------------------------------------------------------------------- 

#!/usr/bin/env python



# This section of code opens the file for reading

str = 'myFile'

file = open(str, 'r')

position = 0



# this outer loop uses xreadlines to read in all of the lines of the file



for line in file.xreadlines():

  data = line.rstrip().split(':')

  print "The line I would like to print is", data[0]

  theFileSeek = file.seek(position)



  #this iner loop reads through the file and loops through

  #until count ==5. It then saves the position of the file

  #when count ==5, and passes that info to the inner loop



  count = 0

  while 1:

    position = file.tell()

    line = file.readline()

data = line.rstrip().split(':')

    count = count + 1



    if count == 5:

      file.seek(position)

      break

  #end while inner loop

#end while outer loop



file.close()



------------------------------------------------------------------



The code that does not work gives me this output:



The line I would like to print is 1

The line I would like to print is 2

The line I would like to print is 3

The line I would like to print is 4

The line I would like to print is 5

The line I would like to print is 6

The line I would like to print is 7

The line I would like to print is 8

The line I would like to print is 9

The line I would like to print is 10

The line I would like to print is 11



Please help!! How do I use the xreadlines() command (for the outer and inner
loop) to accomplish this? Why doesn't xreadlines() and file.seek() work
together? Or do they? I need to use xreadlines() instead of readline()
because the files I will be processing are huge, and xreadlines() processes
faster than readline().  Thanks, and sorry for the long post!


-- 
Remove ".nospam" from e-mail address to reply





More information about the Python-list mailing list