473,406 Members | 2,451 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,406 software developers and data experts.

for line in file weirdness

Hello,

here's a strange bug (?) I've came across (using Python 2.2):

# loop_1
for line in file:
if some_condition(line): break
do_something()

# loop_2
for line in file:
do_something_else()

The problem is, that loop_2 doesn't resume where loop_1 left off, but
skips many lines (a block's worth or so) before continuing.

Why is this? Is reading from a file non-reentrant?

It is always possible to slurp the whole file content into a list, and
then iterate through the list, but I want to handle HUGE files too.

Thanks,
-cpghost.

--
Cordula's Web. http://www.cordula.ws/

Jul 18 '05 #1
4 4637
"Cordula's Web" <cp*****@cordula.ws> wrote:
here's a strange bug (?) I've came across (using Python 2.2):

# loop_1
for line in file:
if some_condition(line): break
do_something()

# loop_2
for line in file:
do_something_else()

The problem is, that loop_2 doesn't resume where loop_1 left off, but
skips many lines (a block's worth or so) before continuing.

Why is this? Is reading from a file non-reentrant?


as mentioned in the documentation, the iterator interface (which is used by the
for-in machiner) uses a read-ahead buffer. in 2.2, whenever you enter a new
loop, a new read-ahead buffer is created, and it starts where the last one ended,
rather than right after the last line you read.

to get more reliable results in 2.2, you can create the iterator outside the loop,
and loop over the iterator object instead of the file itself.

file = iter(open(...))
for line in file:
if some_condition(line): break
do_something()
for line in file:
do_something_else()

(iirc, this quirk was fixed in 2.3)

</F>

Jul 18 '05 #2
I think what you need to do is to have a nested if_else statment:
for line in filelines:
if some_condition : break
else: do_something_else

If the if statment is excuted then break return to for_loop
else do something different then return to for_loop.
When I read from a file I read the whole file into a variable then
work form the variable

file = open('InputString','r') # open file
for reading only
filelines = map(string.strip,file.readlines()) #remove newlines
for string

Then you can just use the variable filelines and loop through as much
as you like. If I can help you can email me at se******@mac.com
I also use SKYPE username servando_garcia
Hope this helped.

Cordula's Web wrote:
Hello,

here's a strange bug (?) I've came across (using Python 2.2):

# loop_1
for line in file:
if some_condition(line): break
do_something()

# loop_2
for line in file:
do_something_else()

The problem is, that loop_2 doesn't resume where loop_1 left off, but
skips many lines (a block's worth or so) before continuing.

Why is this? Is reading from a file non-reentrant?

It is always possible to slurp the whole file content into a list, and then iterate through the list, but I want to handle HUGE files too.

Thanks,
-cpghost.

--
Cordula's Web. http://www.cordula.ws/


Jul 18 '05 #3
A read-ahead buffer? Yes, that would explain it. Sorry, I missed this
piece of information in the documentation.

Thanks to all who replied.

Jul 18 '05 #4
Thanks :)

Reading everything into a variable was not an option, due to some very
large files. Creating the iterator only once, as Fredrik suggested,
solved the problem nicely.

Again many thanks for your great support!

Jul 18 '05 #5

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

Similar topics

15
by: Corne' Cornelius | last post by:
Hi, I'm experiencing some weirdness in a program, when subtracting 2 (double)'s which should result in 0, but instead it returns -1.11022e-16. It looks to me that changing the double x_step...
11
by: Johnny Two Dogs | last post by:
I'm strictly concerned with IE, so cross-browser compatibilty isn't necessary. If you view the code below, I almost get exactly what I'm looking for: - A table of four cells that...
1
by: (Pete Cresswell) | last post by:
TabControl on the right side of a form with two tabs: Tab #1 contains two subforms that show some dynamic query results. Tab #2 contains a ListView that gets dynamically populated as the user...
9
by: AP | last post by:
Is there anyway to determine what the modified or create date is for a file? In my example I am uploading a text file using the following to select the filename: GetOpenFileName Lib "COMDLG32.DLL"....
2
by: Corne' Cornelius | last post by:
Hi, When you open a file for writing/appending with open() or fopen(), and you have multiple applications that might want to write to the same file at the same time, could that cause weirdness...
1
by: VB Programmer | last post by:
My development machine has been working perfectly, writing ASP.NET apps, etc... I just went to open a project and it said: "Visual Studio .NET has detected that the specified web server is not...
5
by: David Thielen | last post by:
Hi; I am creating png files in my ASP .NET app. When I am running under Windows 2003/IIS 6, the file is not given the security permissions it should have. It does not have any permission for...
1
by: daokfella | last post by:
I have a web usercontrol on which is a Webcontrols.FileUpload control. I expose the PostedFile property as a readonly property of the usercontrol. That way, a page has access to it. However,...
1
by: =?ISO-8859-1?Q?thib=B4?= | last post by:
Hi, Taking a closer look to highlight_file() lastly, I found out that its line break policy was a bit.. strange. <?php # highlight_file() line breaks test $f = fopen('highlight_file_test',...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.