Skip to content

Instantly share code, notes, and snippets.

@anna-hope
Last active December 17, 2015 22:19
Show Gist options
  • Save anna-hope/5681117 to your computer and use it in GitHub Desktop.
Save anna-hope/5681117 to your computer and use it in GitHub Desktop.
werkzeug secure_filename fix python 3
if isinstance(filename, text_type):
from unicodedata import normalize
filename = normalize('NFKD', filename).encode('ascii', 'ignore')
if not PY2:
filename = filename.decode('ascii')
for sep in os.path.sep, os.path.altsep:
if sep:
filename = filename.replace(sep, ' ')
filename = str(_filename_ascii_strip_re.sub('', '_'.join(
filename.split()))).strip('._')
# on nt a couple of special files are present in each folder. We
# have to ensure that the target file is not such a filename. In
# this case we prepend an underline
if os.name == 'nt' and filename and \
filename.split('.')[0].upper() in _windows_device_files:
filename = '_' + filename
return filename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment