Specific question about readlines versus xreadlines

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pernell Williams

    Specific question about readlines versus xreadlines

    Hi all:



    Thank you for your responses. I have a more specific question about
    "file.seek( ) and file.readline() " versus "file.seek( ) and file.xreadlines ".



    When I have the following code:



    #!/usr/bin/env python



    #opening the file for reading

    str = 'myShortFile'

    file = open(str, 'r')

    counter = 0

    #outer loop using file.readline



    while 1:

    line = file.readline()

    if line == '':

    break

    data = line

    print data

    file.seek(12)

    # end while



    And the input file is:

    abc

    def

    ghi



    The above code with the readline statement is:

    abc



    In other words, the code reads the first line of the file, goes to position
    12 (which is the end of the last line of the file), and then during the
    second iteration of the while loop, the position of the file is at 12.



    Now, with xreadlines(), it is a different story. I have this code:



    #!/usr/bin/env python



    #opening the file for reading

    str = 'myShortFile'

    file = open(str, 'r')

    counter = 0



    #outer loop using file.xreadlines



    for line in file.xreadlines ():

    data = line

    print data

    file.seek(12)

    # end while



    and if I have the same input file as above, I get this output:



    abc



    def



    ghi



    In other words, even though I have the "file.seek( 12)" statement in the
    loop, the position is NOT changed to 12 during the 2nd iteration of the
    while loop.



    Is there ANYTHING I can do in this "xreadlines ()" loop to make it go to
    position 12 in the second iteration?



    Thanks for your help!



    -Pernell




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


  • Erik Max Francis

    #2
    Re: Specific question about readlines versus xreadlines

    Pernell Williams wrote:
    [color=blue]
    > Thank you for your responses. I have a more specific question about
    > "file.seek( ) and file.readline() " versus "file.seek( ) and
    > file.xreadlines ".[/color]

    The latter won't work, since F.xreadlines buffers its reads.

    --
    __ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
    / \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    \__/ Everyone wants to look good at his own funeral.
    -- Louis Wu

    Comment

    • Garry Knight

      #3
      Re: Specific question about readlines versus xreadlines

      Erik gave you the correct answer. I just thought I'd point out a few other
      things about your code:

      In message <c3aq2p$6v6$1@n ews01.intel.com >, Pernell Williams wrote:
      [color=blue]
      > #opening the file for reading
      >
      > str = 'myShortFile'[/color]

      Did you know that str() is a Python function that returns the string
      representation of an object. To see how it works, run Python interactively
      and pass it some objects:
      print str(3)
      print str('hello')
      d = {"one": 1, "two": 2, "three": 3}
      print str(d)
      In other words, it's not a good idea to redefine str in your code as it
      hides the original function making it unusable. Also...
      [color=blue]
      > file = open(str, 'r')[/color]

      In Python file() is a function that happens to be an alias of open(). So if
      you redefine it like this you can't do:
      f = file('myFile', 'r')
      This is OK if you only intend to use open() but redefining existing Python
      names is still arguably not good practice.
      [color=blue]
      > while 1:
      >
      > line = file.readline()
      >
      > if line == '':
      >
      > break[/color]

      The usual way to read lines from a file is like this (assuming you followed
      my previous suggestion and stopped using 'file' for the filename:
      for line in f.readlines():
      # process line
      This will loop until line == '' so you don't even need the break statement.
      And you can make it shorter still:
      for line in f:
      # process line
      And you can shorten the whole process further still by combining the open
      with the read:
      for line in open(filename):
      # process line

      I realise that your example is specifically comparing and contrasting the
      readline() function with the xreadlines() function, but I wasn't sure if
      you were aware of the different ways of doing the same thing. If you were,
      please ignore the above paragraph: hopefully it will prove useful to
      newcomers to Python.
      [color=blue]
      > data = line
      >
      > print data[/color]

      I'm curious as to why you didn't just do 'print line' rather than assigning
      it to 'data' first.

      --
      Garry Knight
      garryknight@gmx .net ICQ 126351135
      Linux registered user 182025

      Comment

      Working...