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

Larry Bates lbates at swamisoft.com
Mon Mar 15 18:03:31 EST 2004


Pernell,

If I'm understanding your code correctly you need
to look closely at the .seek method.  The argument
to this is bytes (not lines).  Unless you have fixed
length lines, there is no "quick" way to go "seek" to
an arbitrary line in a file.

seek() and xreadlines do seem to work together:

from xreadlines import xreadlines

f=open(r"c:\readme.txt)
f.seek(1000)   # Move down 1000 bytes
for line in xreadlines(f):
    print l

This prints all the lines to the end of the file
beginning 1000 characters from the beginning of
the file.

Hope information helps.

Larry Bates
Syscon, Inc.


"Pernell Williams" <pernell.h.williams.nospam at intel.com> wrote in message
news:c34vpp$7s3$1 at news01.intel.com...
>
> 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