Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Reading a file and resuming reading.

Reply
Thread Tools

Reading a file and resuming reading.

 
 
Karim Ali
Guest
Posts: n/a
 
      05-25-2007
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

 
Reply With Quote
 
 
 
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      05-25-2007
In <(E-Mail Removed)>, 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
 
Reply With Quote
 
 
 
 
Hrvoje Niksic
Guest
Posts: n/a
 
      05-25-2007
"Karim Ali" <(E-Mail Removed)> 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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pausing/ Resuming Thread Using Tool From java.util.concurrent. yccheok Java 1 03-28-2008 02:55 PM
Suspending and resuming JNI thread ganeshamutha@gmail.com Java 8 12-02-2006 07:41 AM
Resuming a program's execution after correcting error Sheldon Python 13 10-05-2006 07:15 PM
Resuming scroll position Emmanuel ASP .Net 4 06-03-2005 02:55 PM
Dell laptop loses (won't re-establish) wireless connection when resuming from standby Oxford Systems Computer Support 7 03-14-2005 03:23 AM



Advertisments