Skip to main content

A permissive filename sanitizer.

Project description

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")

> sanitize("this𓀦filenameḜisあactually...valid.txt")

> sanitize("def.")

> sanitize("NUL")

> sanitize("..")


  • 1.2.0

    • Get rid of os dependent checks; ensure uniform behaviour
    • Now works on long filenames where the non-extensions part consists of only dots
  • 1.1.0

    • Try to preserve filename extensions if possible
  • 1.0.1

    • First release (as 1.0.1 due to a version number mix-up in 1.0.0)
  • 1.0.0-dev3

    • Black list low code point characters (<32)
  • 1.0.0-dev1

    • First version

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sanitize_filename-1.2.0.tar.gz (3.8 kB view hashes)

Uploaded Source

Built Distribution

sanitize_filename-1.2.0-py3-none-any.whl (3.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page