Reading a file and resuming reading.

Discussion in 'Python' started by Karim Ali, May 25, 2007.

  1. Karim Ali

    Karim Ali Guest

    Hi,

    Simple question. Is it possible in python to write code of the type:

    -----------------------------
    while not eof <- really want the EOF and not just an empty line!
    readline by line
    end while;
    -----------------------------

    What I am using now is the implicit for loop after a readlines(). I don't
    like this at all as a matter of opinion (my background is C++).

    But also, in case for one reason or another the program crashes, I want to
    be able to rexecute it and for it to resume reading from the same position
    as it left. If a while loop like the one above can be implemented I can do
    this simply by counting the lines!

    I appreciate any help.

    Karim

    _________________________________________________________________
    Windows Live Hotmail. Now with better security, storage and features.
    www.newhotmail.ca?icid=WLHMENCA149
    Karim Ali, May 25, 2007
    #1
    1. Advertising

  2. In <>, Karim Ali
    wrote:

    > Simple question. Is it possible in python to write code of the type:
    >
    > -----------------------------
    > while not eof <- really want the EOF and not just an empty line!
    > readline by line
    > end while;
    > -----------------------------


    while True:
    line = f.readline()
    if not line:
    break
    # Do something with `line`.

    > What I am using now is the implicit for loop after a readlines(). I don't
    > like this at all as a matter of opinion (my background is C++).


    Both look ugly to Pythonistas too. Files are iterable:

    for line in f:
    # Do something with `line`.

    > But also, in case for one reason or another the program crashes, I want to
    > be able to rexecute it and for it to resume reading from the same position
    > as it left. If a while loop like the one above can be implemented I can do
    > this simply by counting the lines!


    for line_nr, line in enumerate(f):
    # Do something with `line_nr` and `line`.

    Ciao,
    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, May 25, 2007
    #2
    1. Advertising

  3. "Karim Ali" <> writes:

    > -----------------------------
    > while not eof <- really want the EOF and not just an empty line!
    > readline by line
    > end while;
    > -----------------------------


    for line in open_file:
    ...

    It will stop on EOF, not on empty line.

    > But also, in case for one reason or another the program crashes, I
    > want to be able to rexecute it and for it to resume reading from the
    > same position as it left. If a while loop like the one above can be
    > implemented I can do this simply by counting the lines!


    If you open the file in binary mode, you can easily keep track of the
    position in file:

    bytepos = 0
    with file(filename) as f:
    for line in f:
    ... process line ...
    bytepos += len(line)

    If you need to restart the operation, simply seek to the previously
    known position:

    # restart with old bytyepos
    with file(filename) as f:
    f.seek(bytepos)
    for line in f:
    ... process line ...
    bytepos += len(line)
    Hrvoje Niksic, May 25, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Emmanuel

    Resuming scroll position

    Emmanuel, Jun 3, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    2,699
    Emmanuel
    Jun 3, 2005
  2. Sheldon
    Replies:
    13
    Views:
    591
    Scott David Daniels
    Oct 5, 2006
  3. Replies:
    8
    Views:
    1,182
  4. iMath
    Replies:
    0
    Views:
    121
    iMath
    Aug 7, 2013
  5. iMath
    Replies:
    0
    Views:
    114
    iMath
    Aug 7, 2013
Loading...

Share This Page