August 2018
Intermediate to advanced
366 pages
10h 14m
English
Whenever we process a filename, we should decode it according to the expected filesystem encoding. If we fail (because it's not stored in the expected encoding), we must still be able to put it into str without corrupting it, so that we can open that file even though we can't read its name:
def decode_filename(fname):
fse = sys.getfilesystemencoding()
return fname.decode(fse, "surrogateescape")