Python 2.6, File read() problems in Windows Xp

Chris Rebert clp2 at rebertia.com
Fri May 8 01:38:10 EDT 2009


On Thu, May 7, 2009 at 10:04 PM, Li Wang <li.wang.d at gmail.com> wrote:
> Hi all:
>
> I am trying to read a non-text file as a string by using Python
> read(), however, it seems there is some thing wrong with it. I can use
> read() on text file correctly, but unable to read .xls file correctly.
> (The program can read any file correctly in Fedora 10)
>
> Any idea how to solve this problem?

You need to add a "b" to the `flags` argument to open() when you open
the file, e.g. open("the_file.xls", "rb")
Unlike *nix, Windows differentiates between binary and text files,
hence the need for the "b" flag to specify which you're dealing with.

Further info: http://docs.python.org/library/functions.html#open

Cheers,
Chris
-- 
http://blog.rebertia.com



More information about the Python-list mailing list