Mailbox, tell and read

Phil Mayes nospam at bitbucket.com
Sat Jul 3 03:00:55 EDT 1999


Paul Prescod wrote in message <377AAE02.E8591727 at prescod.net>...
>The mailbox message objects simulate a file interface by seeking through a
>mailbox. A read() returns the entire message. It does this by seeking to
>the start, reading to the message end, and returning the data. The problem
>is that message offsets are derived by file.tell() which I presume is in
>terms of bytes. But then it does a read() based on the offsets and read()
>is in terms of characters.
>
>These don't match up on Windows because of CR/LF pairs. I think that the
>right fix is to never use tell() and instead keep track of the location by
>counting characters. Does that make sense?


I am working with multiple mails in a single file, and found that using
seek/tell on the file in text mode didn't allow me to extract a single mail,
so I use binary, but I have to convert internally else quopri leaves =CRLF
inside the string.  Code fragment:
    # read the body from the mbx file with 'rb' to get the correct
    # amount of data in, then convert CRLF to LF else quopri fails.
    fmbx = open(mbxfile, 'rb')
    fmbx.seek(self._offbody)
    s = fmbx.read(self._offend - self._offbody)
    return string.replace(s, '\r\n', '\n') # runs in 2/3 the time of
    # ... joinfield/splitfield
--
Phil Mayes    pmayes AT olivebr DOT com








More information about the Python-list mailing list