473,498 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

file tell in a for-loop

I was trying to map various locations in a file to a dictionary. At
first I read through the file using a for-loop, but tell() gave back
weird results, so I switched to while, then it worked.

The for-loop version was something like:
d = {}
for line in f:
if line.startswith('>'): d[line] = f.tell()

And the while version was:
d = {}
while 1:
line = f.readline()
if len(line) == 0: break
if line.startswith('>'): d[line] = f.tell()
In the for-loop version, f.tell() would sometimes return the same
result multiple times consecutively, even though the for-loop
apparently progressed the file descriptor. I don't have a clue why
this happened, but I switched to while loop and then it worked.

Does anyone have any ideas as to why this is so?

Thanks,
Magdoll
Nov 18 '08 #1
3 2883
On Nov 19, 7:00*am, Magdoll <magd...@gmail.comwrote:
I was trying to map various locations in a file to a dictionary. At
first I read through the file using a for-loop, but tell() gave back
weird results, so I switched to while, then it worked.

The for-loop version was something like:
* * * * * * * * d = {}
* * * * * * * * for line in f:
* * * * * * * * * * * * *if line.startswith('>'): d[line] = f.tell()

And the while version was:
* * * * * * * * d = {}
* * * * * * * * while 1:
* * * * * * * * * * * * line = f.readline()
* * * * * * * * * * * * if len(line) == 0: break
* * * * * * * * * * * * if line.startswith('>'): d[line] = f.tell()

In the for-loop version, f.tell() would sometimes return the same
result multiple times consecutively, even though the for-loop
apparently progressed the file descriptor. I don't have a clue why
this happened, but I switched to while loop and then it worked.

Does anyone have any ideas as to why this is so?

Thanks,
Magdoll
got bitten by that too a while back
the for line in f reads ahead so your f.tell would not be the position
of the end of the line
had to use a while True loop instead also
Nov 19 '08 #2
Magdoll wrote:
I was trying to map various locations in a file to a dictionary. At
first I read through the file using a for-loop, but tell() gave back
weird results, so I switched to while, then it worked.

The for-loop version was something like:
d = {}
for line in f:
if line.startswith('>'): d[line] = f.tell()

And the while version was:
d = {}
while 1:
line = f.readline()
if len(line) == 0: break
if line.startswith('>'): d[line] = f.tell()
In the for-loop version, f.tell() would sometimes return the same
result multiple times consecutively, even though the for-loop
apparently progressed the file descriptor. I don't have a clue why
this happened, but I switched to while loop and then it worked.

Does anyone have any ideas as to why this is so?
I suspect that at least the iterator version uses internal
buffering, so the tell() call returns the current buffer
read-location, not the current read location. I've also had
problems with tell() returning bogus results while reading
through large non-binary files (in this case about a 530 meg
text-file) once the file-offset passed some point I wasn't able
to identify. It may have to do with newline translation as this
was python2.4 on Win32. Switching to "b"inary mode resolved the
issue for me.

I created the following generator to make my life a little easier:

def offset_iter(fp):
assert 'b' in fp.mode.lower(), \
"offset_iter must have a binary file"
while True:
addr = fp.tell()
line = fp.readline()
if not line: break
yield (addr, line.rstrip('\n\r'))

That way, I can just use

f = file('foo.txt', 'b')
for offset, line in offset_iter(f):
if line.startswith('>'): d[line] = offset

This bookmarks the *beginning* (I think your code notes the
*end*) of each line that starts with ">"

-tkc

Nov 19 '08 #3
Gotcha. Thanks!

Magdoll

On Nov 19, 2:57*am, Tim Chase <python.l...@tim.thechases.comwrote:
Magdoll wrote:
I was trying to map various locations in a file to a dictionary. At
first I read through the file using a for-loop, buttell() gave back
weird results, so I switched to while, then it worked.
The for-loop version was something like:
* * * * * * * * d = {}
* * * * * * * * for line in f:
* * * * * * * * * * * * *if line.startswith('>'): d[line] = f.tell()
And the while version was:
* * * * * * * * d = {}
* * * * * * * * while 1:
* * * * * * * * * * * * line = f.readline()
* * * * * * * * * * * * if len(line) == 0: break
* * * * * * * * * * * * if line.startswith('>'): d[line] = f.tell()
In the for-loop version, f.tell() would sometimes return the same
result multiple times consecutively, even though the for-loop
apparently progressed the file descriptor. I don't have a clue why
this happened, but I switched to while loop and then it worked.
Does anyone have any ideas as to why this is so?

I suspect that at least the iterator version uses internal
buffering, so thetell() call returns the current buffer
read-location, not the current read location. *I've also had
problems withtell() returning bogus results while reading
through large non-binary files (in this case about a 530 meg
text-file) once the file-offset passed some point I wasn't able
to identify. *It may have to do with newline translation as this
was python2.4 on Win32. *Switching to "b"inary mode resolved the
issue for me.

I created the following generator to make my life a little easier:

* *def offset_iter(fp):
* * *assert 'b' in fp.mode.lower(), \
* * * *"offset_iter must have a binary file"
* * *while True:
* * * *addr = fp.tell()
* * * *line = fp.readline()
* * * *if not line: break
* * * *yield (addr, line.rstrip('\n\r'))

That way, I can just use

* *f = file('foo.txt', 'b')
* *for offset, line in offset_iter(f):
* * *if line.startswith('>'): d[line] = offset

This bookmarks the *beginning* (I think your code notes the
*end*) of each line that starts with ">"

-tkc
Nov 19 '08 #4

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

Similar topics

5
10123
by: simon place | last post by:
is the code below meant to produce rubbish?, i had expected an exception. f=file('readme.txt','w') f.write(' ') f.read() ( PythonWin 2.3 (#46, Jul 29 2003, 18:54:32) on win32. ) I got...
8
2983
by: Peter Abel | last post by:
Hi all, I'm working under W2k with Python 2.2.2 (#37, Oct 14 2002, 17:02:34) on win32 I have a file *test_data.txt* with the following content: 0123456789 0123456789 abcdefghi...
2
6442
by: Chris McAvoy | last post by:
Is this a bug? (file is an open text file): >>> for i in range(0,5): .... var = file.next() .... file.tell() .... 1675L 1675L 1675L 1675L
35
2619
by: munish.nr | last post by:
Hi All, I want to know the size of file (txt,img or any other file). i knoe only file name. how i can acheive this. does anybody is having idea about that. plz help. rgrds, Munish Nayyar
26
2966
by: Michel Rouzic | last post by:
I have a binary file used to store the values of variables in order to use them again. I easily know whether the file exists or not, but the problem is, in case the program has been earlier...
8
435
by: emanshu, Munish Nayyar | last post by:
HI all, I an designing an application in C++. i want to open file requested by end user but i want to reflect an error to user if file is already opened by some other application.. will anybody...
5
8254
by: Miaaa Mukherjee | last post by:
CAN anyone tell me how can i restore the .BAK extension file ??Please tell me the steps what should be done to restore the file as i m not at all aware of how to restore the .BAK file.Can i directly...
2
1909
by: js | last post by:
Hi list. I'm writing a tail -f like program in python and I found file.read() doesn't work as I think it should. Here's the code illustrating my problem. ### #!/usr/bin/env python import...
1
2152
by: Bob Jones | last post by:
I am using ASP.NET 2.0 w/ c#. I have an Http handler that intercepts any request labeled .file. I am trying to have this handler serve up a file from a remote location (public) and I do not want to...
13
4460
by: thomas.mertes | last post by:
Hello Recently I discovered some problem. I have some C code which determines how many bytes are available in a file. Later I use this information to malloc a buffer of the correct size before...
0
7126
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
7005
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
7210
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...
1
6891
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...
1
4916
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...
0
4595
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1424
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
659
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.