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.

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.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.1.0.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sanitize_filename-1.1.0-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file sanitize_filename-1.1.0.tar.gz.

File metadata

  • Download URL: sanitize_filename-1.1.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.5

File hashes

Hashes for sanitize_filename-1.1.0.tar.gz
Algorithm Hash digest
SHA256 a13a3039658d09d1e573bb326d0f8628da27262996634708dc91b9f10a62a657
MD5 c4c89da6b81d3db94af464a92b73041b
BLAKE2b-256 00cea71fc7f1889080e74d9112b3adb0f85877ceda9743d7bac7ae7b78c2e5cf

See more details on using hashes here.

File details

Details for the file sanitize_filename-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: sanitize_filename-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.5

File hashes

Hashes for sanitize_filename-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 65fc22e3c0396a3875fbfde326a921b01e5ebd28f04ba38c27b4c278978e7e45
MD5 206a7b95f2e93d7289220eb40f27deba
BLAKE2b-256 1ab78c7d3a8205ce01e5d5ed7b14aa78a9e51c733f1eb84acea95225b6a6091a

See more details on using hashes here.

Supported by

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