Check if a file or extension is text, and iterate over 300+ known text file types including source code, markup, data formats, and configuration files. Python port of text-extensions npm package.
- Immutable collection of hundreds of known text file extensions
- Fast membership checks using
frozenset - Case-insensitive and dot-aware checks
- Works for both extensions and full file paths
- Supports dotfiles (e.g.,
.gitignore) - Zero dependencies, minimal overhead
pip install text-extensionsOr using uv:
uv add text-extensionsfrom text_extensions import is_text_extension
is_text_extension("txt") # True
is_text_extension(".py") # True (dot-aware)
is_text_extension("JS") # True (case-insensitive)
is_text_extension("png") # Falsefrom text_extensions import is_text_path
is_text_path("document.txt") # True
is_text_path("/path/to/file.PY") # True (case-insensitive)
is_text_path("script.js") # True
is_text_path("image.png") # False
is_text_path(".gitignore") # True (dotfile support)from text_extensions import TEXT_EXTENSIONS, TEXT_EXTENSIONS_LOWER
# TEXT_EXTENSIONS is a frozenset of all known text extensions
print(len(TEXT_EXTENSIONS)) # Number of supported extensions
"txt" in TEXT_EXTENSIONS # True
"png" in TEXT_EXTENSIONS # False
# TEXT_EXTENSIONS_LOWER contains all extensions in lowercase
# Useful for case-insensitive lookups without calling .lower() repeatedly
"TXT" in TEXT_EXTENSIONS_LOWER # True (case-insensitive)The package includes support for hundreds of text file extensions, including:
- Source Code: py, js, ts, java, c, cpp, go, rs, rb, php, and more
- Markup: html, xml, md, markdown, json, yaml, yml, and more
- Stylesheets: css, scss, sass, less, styl, and more
- Configuration: ini, conf, cfg, json, yaml, toml, and more
- Data Formats: csv, tsv, json, xml, sql, and more
- Documentation: txt, md, rst, tex, and more
- Scripts: sh, bash, zsh, fish, bat, cmd, and more
- And many more...
This package is a Python port of the text-extensions npm package by Sindre Sorhus.
See CHANGELOG.md for a detailed list of changes and version history.
Contributions are welcome! Please read our Contributing Guide for details on our code of conduct, development setup, and the process for submitting pull requests.
If you find this library useful, please consider:
- ⭐ Starring the repository on GitHub to help others discover it.
- 💖 Sponsoring to support ongoing maintenance and development.
Become a Sponsor on GitHub | Support on Patreon
MIT License - see LICENSE file for details.
Y. Siva Sai Krishna
- GitHub: @ysskrishna
- LinkedIn: ysskrishna