views:

50

answers:

1

I have a script in python that needs to read iso-8859-1 files and also write in that encoding.

Now I am running the script in an environment with all locales set at utf-8. Is there a way to define in my python scripts that all file acces have to use the iso-8859-1 encoding?

+4  A: 

Python doesn't really listen to the environment when it comes to reading and writing files in a particular encoding. It only listens to the environment when it comes to encoding unicode written to stdout, if stdout is connected to a terminal.

When reading and writing files in Python 2.x, you deal with bytestrings (the str type) by default. They're encoded data. You have to decode the data you read by hand, and encode what you want to write. Or you can use codecs.open() to open the files, which will do the encoding for you.

In Python 3.x, you open files either in binary mode, in which case you get bytes, or you open it in text mode, in which case you should specify an encoding just like with codecs.open() in Python 2.x.

None of these are affected by environment variables; you either read bytes, or you specify the encoding.

Thomas Wouters