def safe_filename(text, max_length=200):
"""Sanitizes filenames for many operating systems.
:params text: The unsanitized pending filename.
"""
# Tidy up ugly formatted filenames.
text = text.replace('_', ' ')
text = text.replace(':', ' -')
# NTFS forbids filenames containing characters in range 0-31 (0x00-0x1F)
ntfs = [chr(i) for i in range(0, 31)]
# Removing these SHOULD make most filename safe for a wide range of
# operating systems.
paranoid = ['\"', '\#', '\$', '\%', '\'', '\*', '\,', '\.', '\/', '\:',
'\;', '\<', '\>', '\?', '\\', '\^', '\|', '\~', '\\\\']
blacklist = re.compile('|'.join(ntfs + paranoid), re.UNICODE)
filename = blacklist.sub('', text)
return truncate(filename)
评论列表
文章目录