[Python-ideas] Introduce some obvious way to encode and decode filenames from Python code
Stefan Behnel
stefan_ml at behnel.de
Tue Jul 17 06:57:43 CEST 2012
Victor Stinner, 17.07.2012 03:03:
> 2012/7/17 And Clover:
>> But should we be encouraging their use on Windows? I would have thought it
>> the best thing to stick with the Unicode string for paths on NT, so that the
>> native Win32 Unicode APIs are used instead of the ANSI-code-page-bound C
>> stdio. Encoding down to the fsencoding for Windows just means that any path
>> including a character that isn't in the ANSI CP will fail.
>
> os.fsencode() should not be used explicitly on Windows.
>
>> In lieu of some kind of abstract filepath object thatcould represent either
>> bytes or str (depending on platform), how about a function that takes a str
>> and only encodes it to bytes if the platform requires it?
>
> You can use the str (Unicode) type on all platforms with Python 3, so
> use os.fsdecode(). os.listdir(str) does return str filenames on any
> platform for example.
That's not the main use case I see, though. When talking to C libraries,
for example, they will usually require a byte encoded file path and also
return one. Getting the encoding right in this case is really not trivial.
I would expect that the above functions do "the right thing" also on
Windows here, unless the library really has a win32 specific file API (and
that's not likely).
Stefan
More information about the Python-ideas
mailing list