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.

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.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

Release files for sanitize-filename 1.2.0

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.0
File Size Uploaded
sanitize_filename-1.2.0.tar.gz 3.8 kB Details

Built distribution (wheel)

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

Total release size: 7.7 kB

Release files / sanitize_filename-1.2.0.tar.gz

Download URL sanitize_filename-1.2.0.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
e75933e96d426e306eef8c270cc24c3e1971d8715288c9776d801d3d8e7b941a
BLAKE2b-256 checksum
How to use checksums
a31c2757fe1d119304407c501236de0fa7482975f4fbef3d955abc3f7d88c986
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.5

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

Download URL sanitize_filename-1.2.0-py3-none-any.whl
Size 3.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a5be41a4371c84cb4a666a9c3baa70e1b2086a3e50b86c7ba5dd579f5ad2f330
BLAKE2b-256 checksum
How to use checksums
1ef3fd41487943812aedce909d5630c2bcac1b72a8e9e68308195c758f895269
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.5

Release history Release notifications | RSS feed

This release

1.2.0 This release

2 release files

1.1.0

2 release files

1.0.1

2 release files

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