gitignorefile

raw JSON →
1.1.2 verified Fri May 01 auth: no python

A spec-compliant `.gitignore` parser for Python (version 1.1.2). It reads `.gitignore` files and matches file paths against the rules, supporting all features of the Git gitignore specification. The library is actively maintained.

pip install gitignorefile
error AttributeError: module 'gitignorefile' has no attribute 'read_gitignore'
cause Missing import or outdated library version.
fix
Install/upgrade: pip install --upgrade gitignorefile, and import correctly: from gitignorefile import read_gitignore
error TypeError: __init__() takes 1 positional argument but 2 were given
cause Passing raw string to GitignoreFile constructor instead of list of rules.
fix
Use parsed result: rules = read_gitignore('.gitignore'); checker = GitignoreFile(rules)
gotcha The `match` method expects paths relative to the .gitignore file location, not absolute paths.
fix Ensure you pass paths relative to the directory containing the .gitignore file.
gotcha The library only supports regular files, not directories. Calling `match` on a directory path may give unexpected results.
fix Use the `is_ignored_dir` property or check for directory separately.
deprecated The `read_gitignore` function returns a list, but older documentation suggested it returns a different format.
fix Update to version 1.0+; the return type is now a list of rules.
breaking Version 1.0 changed the API from `GitignoreFile(rules)` to accept a list of rule objects, not raw strings.
fix Always pass the output of `read_gitignore` to the constructor.

Basic usage: read .gitignore file and check if a path is ignored.

import os
from gitignorefile import read_gitignore, GitignoreFile

ignores = read_gitignore('.gitignore')
checker = GitignoreFile(ignores)
print(checker.match('some_file.py'))  # True/False