Git Product home page Git Product logo

sanitize-filename's Introduction

Forked from GitLab

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 -U git+https://github.com/MaT-PT/sanitize-filename.git#egg=sanitize_filename

-or-

pip install -U https://github.com/MaT-PT/sanitize-filename/tarball/master#egg=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.2

    • Add option to choose a replacement string for illegal characters
    • Check reserved Windows file names in a case-insensitive manner
    • Update pip command in README to use this GitHub repo
  • 1.2.1

    • Add Python type hints
    • Fix setup.py: use UTF-8 encoding when opening README.md
  • 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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.