Python 3 encoding question: Read a filename from stdin, subsequently open that filename

Nobody nobody at nowhere.com
Tue Nov 30 20:14:05 EST 2010


On Mon, 29 Nov 2010 21:26:23 -0800, Dan Stromberg wrote:

> Does anyone know what I need to do to read filenames from stdin with
> Python 3.1 and subsequently open them, when some of those filenames
> include characters with their high bit set?

Use "bytes" rather than "str". Everywhere. This means reading names from
sys.stdin.buffer (which is a binary stream) rather than sys.stdin (which
is a text stream). If you pass a "bytes" to an I/O function (e.g. open()),
it will just pass the bytes directly to the OS without any decoding.

But really, if you're writing *nix system utilities, you should probably
stick with Python 2.x until the end of time. Using 3.x will just make life
difficult for no good reason (e.g. in 3.x, os.environ also contains
Unicode strings).




More information about the Python-list mailing list