3

I am trying to list all directory files, but without files (or patterns) from .gitignore.

For example, script runs inside main directory:

# .gitignore
dir2/
py.cache

# directory structure
.
├── .gitignore
├── subdir
│   ├── dir1
│   │   ├── file1
│   │   └── py.cache
│   └── dir2
│       ├── file2
│       └── py.cache
└── pydir
    ├── script.py
    └── py.cache

And expected result is:

[Path("subdir/dir1/file1"), Path("pydir/script.py")]

I am looking for something like:

Path(".").iterdir(include_gitignore=True)

# or

for p in Path(".").iterdir():
    if p not in gitignore:
        # ...
Max Smirnov
  • 477
  • 3
  • 10
  • 2
    Check out [pathspec](https://pypi.org/project/pathspec/). It's used in projects such as [black](https://github.com/psf/black/blob/521d1b8129c2d83b4ab49270fe7473802259c2a2/src/black/files.py#L125). – Brad Solomon Jan 17 '22 at 17:14
  • @BradSolomon library that you provided suits well for me. Your answer is solved my problem – Max Smirnov Mar 15 '22 at 20:35

1 Answers1

3

Some code like this should work:

from fnmatch import fnmatch

files = Path("directoryToList").iterdir()
with open(".gitignore") as gitignore_file:
    gitignore = [line for line in gitignore_file.read().splitlines() if line]

filenames = (file for file in files 
             if not any(fnmatch(file, ignore) for ignore in gitignore))
KetZoomer
  • 2,701
  • 3
  • 15
  • 43