os.system and unicode arguments fail on Win32

Martin v. Löwis martin at v.loewis.de
Wed Jan 22 00:41:42 CET 2003

Tim Daneliuk <tundra at tundraware.com> writes:

> All this works fine most of the time.  However, if the user makes
> reference to a file- or directory name which contains an 8-bit
> character (which is legal under Win32), the Windows dialog returns a
> *unicode* string - it apparently assumes that 8-bit data is
> automatically to be made into unicode, rather than returning a byte
> string.

Indeed, Tkinter always returns Unicode strings. Internally, it doesn't
even "know" what the 8-bit representation is (it keeps an UTF-8
representation also, but this is not the "native" 8-bit representation).

> IOW, (and indeed any number of other calls like os.chdir) seem to only
> work with real strings - they'll accept byte strings - but not with unicode
> strings.

This will change in Python 2.3. In many cases, Python 2.2 will also
accept Unicode strings in file system API on Windows. For Python 2.3,
and NT+, all Unicode strings are usable as file names.

This still does not include os.system, or environment variables.

> Is there a way to "depromote" a unicode string into a byte string that
> the os calls can live with, or am I forced to figure out a low-level
> system call (via win32all, I guess) which *should* be able to handle
> this case.

Sure. Just invoke .encode(native encoding) on the Unicode object, to
obtain a byte string.

On Windows, using "mbcs" as the native encoding is correct in most


More information about the Python-list mailing list