Thread: xreadlines ..

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    40
    Rep Power
    10

    xreadlines ..


    Hi,
    I have a question about xreadlines() which we are always use it to do some file processing when we want to read the contain in a file.
    I had read from manry source that say the this module is able to read contain from the file object and will return the contain of the file back to the program.
    My question is :- the xreadlines() is going to read one (1) line from the file object and process it or read the whole contain in the file object, then only do the processing.

    Eg code :
    Code:
    ....
    fp = open("test.txt", "r")
    for lines in fp.xreadlines():
        printHello(lines)
    fp.close()
    ...
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    The xreadlines method is deprecated from Python 2.3, since the file object is now iterable.

    e.g.
    Code:
    ....
    fp = open("test.txt", "r")
    for lines in fp:
        printHello(lines)
    fp.close()
    ...
    In either case it will read a line at a time, not the entire file. If you do want to read the entire file then you can use either file.read() to get the contents as a single string, or file.readlines() to get an array of lines.

    Dave - The Developers' Coach
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You might want to check out this new article:

    http://www.devshed.com/c/a/Python/File-Management-in-Python/
    programming language development: www.netytan.com Hula

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    40
    Rep Power
    10
    so, the xreadlines() is going to read one line from the file & process it ?
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    xreadlines() returns an iterator object (by calling iter()) that is used to read lines from the source file in turn, this allows you to iterate over the file. As Dave said, in Python 2.2+ the file() object is iterable directly so there is no need to call xreadlines() unless you need to maintain compatibility with older versions of Python.

    If you need all the data read in at one go then you would use: the read() method if you wanted a string, and the readlines() method if you wanted a list of all the lines in the file.

    You can find out more about iterators and generators here:

    http://www.python.org/doc/2.4/tut/node11.html#SECTION0011900000000000000000

    Take care,

    Mark.
    programming language development: www.netytan.com Hula

  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    40
    Rep Power
    10
    ok thanks...

IMN logo majestic logo threadwatch logo seochat tools logo