Making safe file names

Andrew Berg bahamutzero8825 at gmail.com
Tue May 7 20:51:24 EDT 2013


On 2013.05.07 19:14, Dave Angel wrote:
> You also need to decide how to handle Unicode characters, since they're 
> different for different OS.  In Windows on NTFS, filenames are in 
> Unicode, while on Unix, filenames are bytes.  So on one of those, you 
> will be encoding/decoding if your code is to be mostly portable.
Characters outside whatever sys.getfilesystemencoding() returns won't be allowed. If the user's locale settings don't support Unicode, my
program will be far from the only one to have issues with it. Any problem reports that arise from a user moving between legacy encodings
will generally be ignored. I haven't yet decided how I will handle artist names with characters outside UTF-8, but inside UTF-16/32 (UTF-16
is just fine on Windows/NTFS, but on Unix(-ish) systems, many use UTF-8 in their locale settings).
> Don't forget that ls and rm may not use the same encoding you're using. 
> So you may not consider it adequate to make the names legal, but you 
> may also want they easily typeable in the shell.
I don't understand. I have no intention of changing Unicode characters.


This is not a Unicode issue since (modern) file systems will happily accept it. The issue is that certain characters (which are ASCII) are
not allowed on some file systems:
 \ / : * ? " < > | @ and the NUL character
The first 9 are not allowed on NTFS, the @ is not allowed on ext3cow, and NUL and / are not allowed on pretty much any file system. Locale
settings and encodings aside, these 11 characters will need to be escaped.
-- 
CPython 3.3.1 | Windows NT 6.2.9200 / FreeBSD 9.1



More information about the Python-list mailing list