[Python-ideas] Introduce some obvious way to encode and decode filenames from Python code

Victor Stinner victor.stinner at gmail.com
Tue Jul 17 16:31:57 CEST 2012


>> Well, how about os.fsencode() and os.fsdecode()?
>>
>> http://docs.python.org/dev/library/os.html#os.fsencode
>
> It's too bad these are not called os.path.encode() and os.path.decode(),
> since they fit so nicely into os.path's charter of manipulating strings
> representing file paths.

os.fsencode()/fsdecode() are not specific to filesystems: you can use
these functions to encode/decode command line arguments, environment
variable, text from/to a console (sys.std*), etc.

The "fs" letters from the name comes from the encoding used by these
functions: sys.get*filesystem*encoding().

For example, os.fsencode() used by the subprocess module and
posixpath.expanduser() modules, and os.fsdecode() is used by
os.get_exec_path() and shutil.rmtree().

Victor



More information about the Python-ideas mailing list