Puzzled: Y am I ending up with extra bytes?

A.Newby deathtospam43423 at altavista.com
Sat Feb 23 14:23:44 EST 2002


Why is this happening? I read large chunks of data from a text file, 
according to byte locations specified on another file, and for some reason, 
this function (below), spits out a few extra bytes.

Here's the code, as entered into the Python shell...... 


    	index = map(string.rstrip, open('D:\cgi-bin\indx.txt').readlines())
    	#this opens the index file, which has precise byte locations of each 
    	#chunk of data I want to extract from the log.txt file, and turns it 
    	#into a list.


    	def fish(end, start, deduct):
	    	chat = open('D:\cgi-bin\log.txt', 'r')
	    	g = int(index[end]) - int(index[start])
	    	chat.seek(int(index[start]))
	    	print chat.read(g - deduct)



Now, if I enter the following into the command line ...

fish(205, 204, 0)

,,, I get about four extra characters. God knows why! So that's why I try 
...

fish(205, 204, 4) 

... And it seems to work perfectly. I can even "fish" up to about about ten 
"index" lines with it. But as soon as I try and fish out any more than 
that, I get the dreaded extra 4 bytes of code again. Why?

Now I know what you're thinking. You're thinking that perhaps my index file 
is corrupted, and hasn't got an accurate account of precisely what's in the 
log file. I suspected that might be the case myself, but ... when I try 
fishing out each chunk of data individually, it works fine! I can even do 
this ...

for x in range(1, 90):
	fish(211+x, 210 + x, 4)

...... without ending up with that extra data I don't want. However, this 
method is too slow for my purposes. Plus, I really wanna know what it is 
that's going wrong.

Can anyone spot it?

BTW, if you're curious, this is part of a chat script I'm putting together. 


cdewin at dingoblue.net.au



More information about the Python-list mailing list