Sunday, November 2, 2008

Python Gotchas and Pitfalls for Windows Programmers

The no.1 pitfall i face is the "\r\n" that windows uses for its text "open" file where else in linux it will be "\n". 
Although this might seems obvious or easy to fix by using "open("xxxxx", mode="wb")" when writing a string to a file, its not as simple as it sounds.

This is because many of the functions that is supposed to be ported to Windows will failed to work, one example is the mbox and maildir, if you use the "import mailbox" and attempt to use maildir to store the files, even if you issue a "rb" on an eml, when you store it via :

    dest_mbox = mailbox.Maildir(path + folder, create=True)   
    msgdata = email.message_from_string(emailtext)        
    new_key = dest_mbox.add(msgdata)        
The stored eml message contains "\r\r\n". This will then be a problem when you try to parse it via the email module.

Who knows what other python provided modules works this way too...