Skip to content

Instantly share code, notes, and snippets.

@agalea91
Created May 22, 2020 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agalea91/bfbb2036547c64923330055b6c7f8cb1 to your computer and use it in GitHub Desktop.
Save agalea91/bfbb2036547c64923330055b6c7f8cb1 to your computer and use it in GitHub Desktop.
def get_valid_filename(s):
"""
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.
# https://github.com/django/django/blob/master/django/utils/text.py
"""
s = str(s).strip().replace(' ', '_')
s = re.sub(r'(?u)[^-\w.]', '', s)
return s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment