GitHub - pre-commit/identify: File identification library for Python · GitHub
https://github.com/pre-commit/identify • 301 KB fetched Open original page
GitHub - pre-commit/identify: File identification library for Python · GitHub
Skip to content
Navigation Menu
Sign in Appearance settings
* Platform
* AI CODE CREATION
* GitHub Copilot Write better code with AI
* GitHub Copilot app Direct agents from issue to merge
* MCP Registry Integrate external tools
* DEVELOPER WORKFLOWS
* Actions Automate any workflow
* Codespaces Instant dev environments
* Issues Plan and track work
* Code Review Manage code changes
* Code Quality Enforce quality at merge
* APPLICATION SECURITY
* GitHub Advanced Security Find and fix vulnerabilities
* Code security Secure your code as you build
* Secret protection Stop leaks before they start
* EXPLORE
* Why GitHub
* Documentation
* Blog
* Changelog
* Marketplace
View all features
* Solutions
* BY COMPANY SIZE
* Enterprises
* Small and medium teams
* Startups
* Nonprofits
* BY USE CASE
* App Modernization
* DevSecOps
* DevOps
* CI/CD
* View all use cases
* BY INDUSTRY
* Healthcare
* Financial services
* Manufacturing
* Government
* View all industries
View all solutions
* Resources
* EXPLORE BY TOPIC
* AI
* Software Development
* DevOps
* Security
* View all topics
* EXPLORE BY TYPE
* Customer stories
* Events & webinars
* Ebooks & reports
* Business insights
* GitHub Skills
* SUPPORT & SERVICES
* Documentation
* Customer support
* Community forum
* Trust center
* Partners
View all resources
* Open Source
* COMMUNITY
* GitHub Sponsors Fund open source developers
* PROGRAMS
* Security Lab
* Maintainer Community
* GitHub Stars
* Archive Program
* REPOSITORIES
* Topics
* Trending
* Collections
* Enterprise
* ENTERPRISE SOLUTIONS
* Enterprise platform AI-powered developer platform
* AVAILABLE ADD-ONS
* GitHub Advanced Security Enterprise-grade security features
* Copilot for Business Enterprise-grade AI features
* Premium Support Enterprise-grade 24/7 support
* Pricing
Search /
Sign in
Sign up Appearance settings
You signed in with another tab or window. Reload to refresh your session.
You signed out in another tab or window. Reload to refresh your session.
You switched accounts on another tab or window. Reload to refresh your session.
Dismiss alert
Uh oh!
There was an error while loading. Please reload this page .
pre-commit
/
identify
Public
*
Uh oh!
There was an error while loading. Please reload this page .
*
Notifications
You must be signed in to change notification settings
*
Fork
179
*
Star
297
*
Code
*
Issues
3
*
Pull requests
8
*
Actions
*
Security and quality
0
*
Insights
Additional navigation options
*
Code
*
Issues
*
Pull requests
*
Actions
*
Security and quality
*
Insights
main
Branches Tags
Go to file
Code Open more actions menu
Latest commit
History
1,069 Commits
1,069 Commits
Folders and files
Name Name Last commit message
Last commit date
.github/ workflows
.github/ workflows
bin
bin
identify
identify
tests
tests
.activate.sh
.activate.sh
.deactivate.sh
.deactivate.sh
.gitignore
.gitignore
.pre-commit-config.yaml
.pre-commit-config.yaml
LICENSE
LICENSE
README.md
README.md
requirements-dev.txt
requirements-dev.txt
setup.cfg
setup.cfg
setup.py
setup.py
tox.ini
tox.ini
View all files
Repository files navigation
*
* README
* MIT license
* Security
More items
identify
File identification library for Python.
Given a file (or some information about a file), return a set of standardized
tags identifying what the file is.
Installation
pip install identify
Usage
With a file on disk
If you have an actual file on disk, you can get the most information possible
(a superset of all other methods):
> >> from identify import identify
> >> identify . tags_from_path ( '/path/to/file.py' )
{ 'file' , 'text' , 'python' , 'non-executable' }
> >> identify . tags_from_path ( '/path/to/file-with-shebang' )
{ 'file' , 'text' , 'shell' , 'bash' , 'executable' }
> >> identify . tags_from_path ( '/bin/bash' )
{ 'file' , 'binary' , 'executable' }
> >> identify . tags_from_path ( '/path/to/directory' )
{ 'directory' }
> >> identify . tags_from_path ( '/path/to/symlink' )
{ 'symlink' }
When using a file on disk, the checks performed are:
* File type (file, symlink, directory, socket)
* Mode (is it executable?)
* File name (mostly based on extension)
* If executable, the shebang is read and the interpreter interpreted
If you only have the filename
> >> identify . tags_from_filename ( 'file.py' )
{ 'text' , 'python' }
If you only have the interpreter
> >> identify . tags_from_interpreter ( 'python3.5' )
{ 'python' , 'python3' }
> >> identify . tags_from_interpreter ( 'bash' )
{ 'shell' , 'bash' }
> >> identify . tags_from_interpreter ( 'some-unrecognized-thing' )
set ()
As a cli
$ identify-cli --help
usage: identify-cli [-h] [--filename-only] path
positional arguments:
path
optional arguments:
-h, --help show this help message and exit
--filename-only
$ identify-cli setup.py ; echo $?
["file", "non-executable", "python", "text"]
0
$ identify-cli setup.py --filename-only ; echo $?
["python", "text"]
0
$ identify-cli wat.wat ; echo $?
wat.wat does not exist.
1
$ identify-cli wat.wat --filename-only ; echo $?
1
Identifying LICENSE files
identify also has an api for determining what type of license is contained
in a file. This routine is roughly based on the approaches used by
licensee (the ruby gem that github uses to figure out the license for a
repo).
The approach that identify uses is as follows:
* Strip the copyright line
* Normalize all whitespace
* Return any exact matches
* Return the closest by edit distance (where edit distance < 5%)
To use the api, install via pip install identify[license]
>>> from identify import identify
>>> identify.license_id( ' LICENSE ' )
'MIT'
The return value of the license_id function is an SPDX id. Currently
licenses are sourced from choosealicense.com .
How it works
A call to tags_from_path does this:
* What is the type: file, symlink, directory? If it's not file, stop here.
* Is it executable? Add the appropriate tag.
* Do we recognize the file extension? If so, add the appropriate tags, stop
here. These tags would include binary/text.
* Peek at the first X bytes of the file. Use these to determine whether it is
binary or text, add the appropriate tag.
* If identified as text above, try to read and interpret the shebang, and add
appropriate tags.
By design, this means we don't need to partially read files where we recognize
the file extension.
About
File identification library for Python
Resources
Readme
MIT license
Security policy
Security policy
Activity
Custom properties
Stars
297 stars
Watchers
4 watching
Forks
179 forks
Report repository
Releases
Sponsor this project
Packages
Used by
Contributors
Languages
Footer
(c) 2026 GitHub, Inc.
Footer navigation
*
Terms
*
Privacy
*
Security
*
Status
*
Community
*
Docs
*
Contact
*
Manage cookies
*
Do not share my personal information
You can’t perform that action at this time.
Links found on this page
- Skip to content [direct]
- Sign in [direct]
- GitHub Copilot Write better code with AI [direct]
- GitHub Copilot app Direct agents from issue to merge [direct]
- MCP Registry Integrate external tools [direct]
- Actions Automate any workflow [direct]
- Codespaces Instant dev environments [direct]
- Issues Plan and track work [direct]
- Code Review Manage code changes [direct]
- Code Quality Enforce quality at merge [direct]
- GitHub Advanced Security Find and fix vulnerabilities [direct]
- Code security Secure your code as you build [direct]
- Secret protection Stop leaks before they start [direct]
- Why GitHub [direct]
- Documentation [direct]
- Blog [direct]
- Changelog [direct]
- Marketplace [direct]
- View all features [direct]
- Enterprises [direct]
- Small and medium teams [direct]
- Startups [direct]
- Nonprofits [direct]
- App Modernization [direct]
- DevSecOps [direct]
- DevOps [direct]
- CI/CD [direct]
- View all use cases [direct]
- Healthcare [direct]
- Financial services [direct]
- Manufacturing [direct]
- Government [direct]
- View all industries [direct]
- View all solutions [direct]
- AI [direct]
- Software Development [direct]
- DevOps [direct]
- Security [direct]
- View all topics [direct]
- Customer stories [direct]
- Events & webinars [direct]
- Ebooks & reports [direct]
- Business insights [direct]
- GitHub Skills [direct]
- Customer support [direct]
- Community forum [direct]
- Trust center [direct]
- Partners [direct]
- View all resources [direct]
- GitHub Sponsors Fund open source developers [direct]
- Security Lab [direct]
- Maintainer Community [direct]
- GitHub Stars [direct]
- Archive Program [direct]
- Topics [direct]
- Trending [direct]
- Collections [direct]
- Copilot for Business Enterprise-grade AI features [direct]
- Premium Support Enterprise-grade 24/7 support [direct]
- Pricing [direct]
- Sign up [direct]
- pre-commit [direct]
- Notifications [direct]
- Issues
3 [direct]
- Pull requests
8 [direct]
- Actions [direct]
- Security and quality
0 [direct]
- Insights [direct]
- Branches [direct]
- Tags [direct]
- 1,069 Commits [direct]
- .github/ workflows [direct]
- bin [direct]
- identify [direct]
- tests [direct]
- .activate.sh [direct]
- .deactivate.sh [direct]
- .gitignore [direct]
- .pre-commit-config.yaml [direct]
- LICENSE [direct]
|
|