DEV Community

Cover image for Python Respect the .gitignore
Waylon Walker
Waylon Walker

Posted on β€’ Originally published at waylonwalker.com

6 2

Python Respect the .gitignore

Many tools such as ripgrep respect the .gitignore file in the directory it's searching in. This helps make it incredibly faster and generally more intuitive for the user as it just searches files that are part of thier project and not things like their virtual environments, node modules, or compiled builds.

Editors like vscode often do not include files that are .gitignored in
their search either.

pathspec is a pattern matching library that implements git's wildmatch
pattern so that you can ignore files included in your .gitignore patterns. You might want this to help make your libraries more performant, or more intuitive for you users.

import pathspec from pathlib import Path

markdown_files = Path().glob('**/*.md') if (Path(".gitignore").exists():
    lines = Path(".gitignore").read_text().splitlines()

    spec = pathspec.PathSpec.from_lines("gitwildmatch", lines)

    markdown_files = [
        file for file in markdown_files if not spec.match_file(str(file))
    ]
Enter fullscreen mode Exit fullscreen mode

pathspec home page


This is my til series, I try to post daily in this series on things that I learn or teach someone. All my tils are posted in this feed on my website.

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post β†’

Top comments (0)

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay