def get_valid_filename(s):
s = str(s).strip().replace('<STR_LIT:U+0020>', '<STR_LIT:_>')<EOL>return re.sub(r'<STR_LIT>', '<STR_LIT>', s)<EOL>
Shamelessly taken from Django.
https://github.com/django/django/blob/master/django/utils/text.py

Return the given string converted to a string that can be used for a clean
filename. Remove leading and trailing spaces; convert other spaces to
underscores; and remove anything that is not an alphanumeric, dash,
underscore, or dot.
>>> get_valid_filename("john's portrait in 2004.jpg")
'johns_portrait_in_2004.jpg'