Last active
December 17, 2015 22:19
-
-
Save anna-hope/5681117 to your computer and use it in GitHub Desktop.
werkzeug secure_filename fix python 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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