newbie: another problem with file reading

Jeff Shannon jeff at ccvcorp.com
Mon Feb 25 20:48:43 EST 2002


Flavian Hardcastle wrote:

> In this instance, _before_ = 8221 , _after_ = 8305.
>
> What this function should do, is read only up to position 8221 in _chat_,
> print a .gif, then read the rest of the _chat_ file. Why isn't it doing
> that?

It's hard to tell, for sure, since we know nothing of the format of your index
file (or your chat_log file, either), and you haven't told us what it *is*
doing.  Is each line of your index file an offset to the start of a post in
the log file?  A few thoughts, based on that assumption:


> def get_posts(num, before):
>     index = map(string.rstrip, open(chat_index).readlines())
>     after = int(str(index[-1]))
>     g = int(str(index[-(num+1)])) #for some reason, need to add +1
>                                   #maybe cuz of the way chat_log and
>                                             #chat_index are set up

As far as why you need to add 1, that really depends on how you're getting num
and what it's supposed to mean (it's not particularly clear from the code
here).  What I'm really puzzled by, here, is the int(str(index[...])) calls.
Your index should already be a list of strings, so I don't see what you're
accomplishing by calling str() on it -- it seems to me that int(index[...])
should work just as well.


>     print int(before)
>     chat = open(chat_log, 'r')
>     chat.seek(g) #seek to where the post the user wants is
>
>     print chat.read(before) #should mean read up to previous position

This reads before number of bytes from chat, starting at position g -- in
other words, you're getting the contents of chat from position g to position g
+ before.  Since you don't say anything about how you've calculated before,
either, it's impossible to tell if this is the behavior you expect, but it
seems like a likely point of misunderstanding.  (Your comment seems to
indicate a mistaken impression, to me.)


>     print "<img src = pointer.gif></br>"
>     print chat.read() #read rest of file.
>     print after

It's not clear what purpose after is supposed to serve, here.  If my guess as
to your index file layout is correct, then it's the offset of the most recent
post in the chat log, but I don't know why you'd want to print it (except
maybe for debugging purposes).

Jeff Shannon
Technician/Programmer
Credit International





More information about the Python-list mailing list