Skip to main content

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. It might be useful for trusted but dirty input, but should not be used with raw user input!

Installation

pip install sanitize_filename

Usage

from sanitize_filename import sanitize


filename = input("Enter a file name:")
filename = sanitize(filename)

Examples:

> 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("..")
'__'

Changelog

  • 1.2.1

    • Fix a bug where a fullwidth character could be used craft path traversal attack
    • Add some missing Windows device names
    • Remove lone surrogates
    • Fix a bug where names could be crafted to end in a dot.
    • Remove C1 range control characters
  • 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

Metadata

Release files for sanitize-filename 1.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sanitize-filename 1.2.1
File Size Uploaded Provenance Actions
sanitize_filename-1.2.1.tar.gz 4.8 kB Without provenance Details for sanitize_filename-1.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sanitize-filename 1.2.1
File Interpreter ABI Platform Actions
sanitize_filename-1.2.1-py3-none-any.whl Python 3 none any Details for sanitize_filename-1.2.1-py3-none-any.whl

Total release size: 9.1 kB

Release files / sanitize_filename-1.2.1.tar.gz

Download URL sanitize_filename-1.2.1.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
e91e6ed73a2a9be8cf9dbf664b47e3e8de7160519fc274f4f42f5d4a5413fdc2
BLAKE2b-256 checksum
How to use checksums
083f77c5838a8c6c89f55c63c3d398a8669a3e72ea9f281be8829ed6d2f3916d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Release files / sanitize_filename-1.2.1-py3-none-any.whl

Download URL sanitize_filename-1.2.1-py3-none-any.whl
Size 4.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
087cc15696133682e98fb62b25053973e90f1dfd02725e7637e5019c982d2fa7
BLAKE2b-256 checksum
How to use checksums
53ec214fb9ce5cbdb823d660fbd64d80cbba94b7aa00f709fd6bf3cade9bb949
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Security for sanitize-filename 1.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page