473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Reading a file and resuming reading.

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

May 25 '07 #1
2 1100
In <ma***************************************@python. org>, 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
May 25 '07 #2
"Karim Ali" <ka****@hotmail.comwrites:
-----------------------------
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)
May 25 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: | last post by:
I am having a major problem with file transfers - they are ending early when the bandwidth tops-out. Smaller files transfer just fine, but large files (12Mb+) can 'abort' the transfer after maybe...
2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
0
by: kajol | last post by:
Hi I am creating a setup project in VS.NET, I have "PluginInstaller.msi" installed in my computer, When I am running this setup program in a test machine where no .NET framework and MDAC2.7 is...
1
by: LP | last post by:
Hi, I developed a console application that is currently in production. It is scheduled to run every morning (very early morning), to move files from one server to another, update SQL db and then...
3
by: Paul Porthouse | last post by:
Hi. I have successfully written an automatic file downloader in VB.NET using webrequest but I can't find any information anywhere on how to read in the filename that the server is sending. ...
4
by: Crouchie1998 | last post by:
How do I download a file & resume from where it left off at a later time/date? Any ideas?
1
by: sushma | last post by:
hi, in my application i want to upload files to http and ftp sites.what i need a safe copy in case of lost connections and also resuming the upload from last broken connection.will it be...
0
by: mike | last post by:
Sound problems resuming from Hibernation I have an MP3 player that plays MP3s off DVD. Uses mci sendstring errreturn = mciSendString("play sound", rs, 128, cb) Been working great for...
4
by: Jassim Rahma | last post by:
hello, i want to know how can i copy a file in a way if it was stopped for a reason it can continue from the last point just like what torrents and file download managers do? thanks..
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.