sanitizefilename#
- sanitizefilename(filename, sub='_', allowspaces=False, asciify=True, strict=False, disallowed=None, aspath=False)[source]#
Takes a potentially Linux- and Windows-unfriendly candidate file name, and returns a “sanitized” version that is more usable.
- Parameters:
filename (str) – the filename to sanitize
sub (str) – the character to substitute unsafe input characters with
allowspaces (bool) – whether to allow spaces in the filename
asciify (bool) – whether to convert the string from Unicode to ASCII
strict (bool) – whether to remove (almost) all non-alphanumeric characters
disallowed (str) – optionally supply a custom list of disallowed characters
aspath (bool) – whether to return a Path object
Example:
bad = 'Nöt*a file&name?!.doc' good = sc.sanitizefilename(bad)
New version 2.0.1: arguments “sub”, “allowspaces”, “asciify”, “strict”, and “disallowed”New version 3.1.1: disallow tabs and newlines even whenstrict=False