os.system and unicode arguments fail on Win32

Tim Daneliuk tundra at tundraware.com
Tue Jan 21 18:20:07 EST 2003


I have a program which, among other things, allows the user to popup a
tkSimpleDialog.askstring() dialog box and enter an arbitrary command
they would like to run. Tkinter apparently maps this dialog onto a
native Win32 dialog which prompts the user and returns their entry. I
then issue an os.system() call on that string.

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.  This makes os.system fail loudly complaining with
the dreaded: UnicodeError: ASCII encoding error: ordinal not in range(128)

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.

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.

Arrrrrrgggghhhhhh...
-- 
------------------------------------------------------------------------------
Tim Daneliuk
tundra at tundraware.com





More information about the Python-list mailing list