A permissive filename sanitizer.
A simple, dependency-free, blacklist-based filename sanitizer, for when you want to keep the original filename.
Note that a blacklist based sanitizer will never be as safe as a whitelist based one. In most cases, your best option is to create a safe filename yourself. Your second safest option is to use a whitelist approach (allowing only certain characters). This sanitizer is useful when you want to keep the original filename, including non ascii characters, whenever possible.
pip install sanitize_filename
from sanitize_filename import sanitize filename = input("Enter a file name:") filename = sanitize(filename)
> sanitize("A/B/C.txt") 'ABC.txt' > sanitize("this𓀦filenameḜisあactually...valid.txt") 'this𓀦filenameḜisあactually...valid.txt' > sanitize("def.") 'def' > sanitize("NUL") '__NUL' > sanitize("..") '__'
- First version
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size sanitize_filename-1.0.0.dev1-py3-none-any.whl (3.6 kB)||File type Wheel||Python version py3||Upload date||Hashes View hashes|
|Filename, size sanitize_filename-1.0.0.dev1.tar.gz (3.2 kB)||File type Source||Python version None||Upload date||Hashes View hashes|
Hashes for sanitize_filename-1.0.0.dev1-py3-none-any.whl
Hashes for sanitize_filename-1.0.0.dev1.tar.gz