Filter filesystem paths based on gitignore-like patterns
pip install py-walk
py-walk
Python library to filter filesystem paths based on gitignore-like patterns.
Example:
from py_walk import walk
from py_walk import get_parser_from_text
patterns = """
**/data/*.bin
!**/data/foo.bin
# python files
__pycache__/
*.py[cod]
"""
# you can get the filtered paths from a directory
for path in walk("some/directory", ignore=patterns):
do_something(path)
# ...or check paths against the patterns manually
parser = get_parser_from_text(patterns, base_dir="some/directory")
if parser.match("file.txt"):
do_something_else()
py-walk can be useful for applications or tools that work with paths and aim to
offer a .gitignore type file to their users. It's also handy for users working
in interactive sessions who need to quickly retrieve sets of paths that must
meet relatively complex constraints.
py-walk tries to achieve 100% compatibility with Git's gitignore (wildmatch) pattern syntax. Currently, it includes more than 500 tests, which incorporate all the original tests from the Git codebase. These tests are executed against
git check-ignoreto ensure as much compatibility as possible. If you find any divergence, please don't hesitate to open an issue or PR.
Installation
To install py-walk, simply use pip:
$ pip install py-walk
Usage
With py-walk, you have the ability to input paths into the library to determine whether they match with a set of gitignore-based patterns. Alternatively, you can directly traverse the contents of a directory, based on a set of conditions that the paths must meet.
walk
To walk through all the contents of a directory, don't provide any constraints:
from py-walk import walk
for path in walk("/some/directory/"):
print(path)
walk accepts the directory to traverse as a strings or as a object from
. It returns objects.