Read a file with open command

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Fri Aug 11 14:03:42 EDT 2006


In <1155314363.231911.74170 at 75g2000cwc.googlegroups.com>, jean-jeanot
wrote:

> Sorry, but the access mode is not binary with XP Windows.

But ods files are binary so you better open them as binary files.  Windows
does a "magical" translation of line ending bytes and stops processing a
file if it hits a \x26 byte if the file was opened in the default text
mode.

> Finally for reading the file it is necessary to use a slash or a double
> backslash. If the file is a simple textfile, using a backslash is
> perhaps not recommended but it is functionning.

The slashes don't have anything to do with the content of the file.  For
both, binary and text files, you have to escape backslashes in literal
strings or sooner or later you will be bitten by a backslash + character
combination that's a valid escape sequence in literal strings.

> >>> file_obj= open ("D:/Mes documents/ADB Anna.ods",'r')
> >>> s = file_obj
> >>> s.readlines()

This doesn't make much sense as `ods` files don't contain text lines but
are binary.  A ZIP archive containing XML files to be more precise.

If you want to read the raw binary data into memory it's better to use::

  file_obj = open('D:/Mes documents/ADB Anna.ods', 'rb')
  data = file_obj.read()
  file_obj.close()

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list