Skip to content

Instantly share code, notes, and snippets.

@wassname
Last active November 30, 2022 14:19
Show Gist options
  • Star 34 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save wassname/1393c4a57cfcbf03641dbc31886123b8 to your computer and use it in GitHub Desktop.
Save wassname/1393c4a57cfcbf03641dbc31886123b8 to your computer and use it in GitHub Desktop.
python convert string to safe filename
"""
Url: https://gist.github.com/wassname/1393c4a57cfcbf03641dbc31886123b8
"""
import unicodedata
import string
valid_filename_chars = "-_.() %s%s" % (string.ascii_letters, string.digits)
char_limit = 255
def clean_filename(filename, whitelist=valid_filename_chars, replace=' '):
# replace spaces
for r in replace:
filename = filename.replace(r,'_')
# keep only valid ascii chars
cleaned_filename = unicodedata.normalize('NFKD', filename).encode('ASCII', 'ignore').decode()
# keep only whitelisted chars
cleaned_filename = ''.join(c for c in cleaned_filename if c in whitelist)
if len(cleaned_filename)>char_limit:
print("Warning, filename truncated because it was over {}. Filenames may no longer be unique".format(char_limit))
return cleaned_filename[:char_limit]
# test
s='fake_folder/\[]}{}|~`"\':;,/? abcABC 0123 !@#$%^&*()_+ clᐖﯫⅺຶ 讀ϯՋ㉘ ⅮRㇻᎠ⍩ 𝁱C ℿ؛ἂeuឃC ᅕ ᑉﺜͧ bⓝ s⡽Հᛕ\ue063 牢𐐥er ᐛŴ n წş .ھڱ df df dsfsdfgsg!zip'
clean_filename(s) # 'fake_folder_abcABC_0123_____clxi_28_DR_C_euC___bn_s_er_W_n_s_.zip'
@ayanbanrj
Copy link

You could perhaps truncate the filename to 255 characters to make it safe for Windows.

@wassname
Copy link
Author

wassname commented Sep 8, 2018

Yeah good point!

@SupinePandora43
Copy link

i'll use it in my addon manager (for transforming steam workshop title to valid fileName)

@ennemosc
Copy link

You could perhaps truncate the filename to 255 characters to make it safe for Windows.

Here's a description on how to overcome the 255-chars limit:
https://stackoverflow.com/questions/36219317/pathname-too-long-to-open/36237176

There one can find a summary and also links to Microsoft describing the reasoning behind that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment