Advertisement
steve-shambles-2109

Clean string to use as a filename

Apr 4th, 2020
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. """"Code snippets vol-49-snippet-244
  2.    Clean string to use as a filename.
  3.  
  4. Download all snippets so far:
  5. https://wp.me/Pa5TU8-1yg
  6.  
  7. Blog: stevepython.wordpress.com
  8.  
  9. requirements:None
  10.  
  11. origin:
  12. https://gist.github.com/wassname/1393c4a57cfcbf03641dbc31886123b8
  13. """
  14. import unicodedata
  15. import string
  16.  
  17. valid_filename_chars = '-_.() %s%s' % (string.ascii_letters, string.digits)
  18. char_limit = 255
  19.  
  20. def clean_filename(filename, whitelist=valid_filename_chars, replace=' '):
  21.     """Clean file."""
  22.     print('Before: ', filename)
  23.     # replace spaces.
  24.     for r in replace:
  25.         filename = filename.replace(r, '_')
  26.  
  27.     # keep only valid ascii chars.
  28.     cleaned_filename = unicodedata.normalize('NFKD', filename).encode('ASCII', 'ignore').decode()
  29.  
  30.     # keep only whitelisted chars.
  31.     cleaned_filename = ''.join(c for c in cleaned_filename if c in whitelist)
  32.     if len(cleaned_filename) > char_limit:
  33.         print('Warning, filename truncated because it was over {}. '
  34.               'Filenames may no longer be unique'.format(char_limit))
  35.     print('After cleaning: ', cleaned_filename)
  36.     return cleaned_filename[:char_limit]
  37.  
  38. clean_filename('fa_/\[]}{}|~`"\':;,/? abcABC 0123 !@#$%^&*()_+ cl!zip')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement