Skip to main content

Filter Paths in Python the easy way.

This library aims to provide simple means to filter file paths.

The base function: filter_file_paths

filter_file_paths takes in three arguments:

  • root_dir: the directory to start searching in

  • path_filter: a function (callable) that should return true for any path/filename encountered that should be kept.

  • relative_paths: default set to True, resulting in relative paths of matched files (with base root_dir).

    If set to False, absolute paths will be returned.

filename_filter

file_path_filter can be a simple function:

def filter(filepath):
   return filepath.lower().endswith('.py'):

# Or, for those that like to use lambda:
lambda x: x.lower().endswith('.py)

Applying a filter like like this on a directory should return only python file filepaths. Easy enough.

But there are advanced usecases. Consider our (simplified) project directory:

.
├── build
│   └── lib
│       └── fpf
│           ├── filters.py
│           ├── __init__.py
│           ├── logger.py
│           └── mixins.py
├── fpf
│   ├── filters.py
│   ├── __init__.py
│   ├── logger.py
│   └── mixins.py
├── LICENSE
├── README.md
├── setup.cfg
├── setup.py
├── tests
│  ├── __init__.py
│  └── test_fpf.py
├── .gitignore
└── .travis.yml

Applying the filter, would give us the following result:

from fpf import file_path_filter

for path in file_path_filter(root_dir='.', path_filter=filter ):
   print(path)
build/lib/fpf/__init__.py
build/lib/fpf/filters.py
...
fpf/__init__.py
fpf/filters.py
...
tests/__init__.py
setup.py
  • Q: But what if I am not interested in build artifacts or tests?

  • A: You can add more conditions to filter(filepath)

  • Q: Does that scale?

  • A: No

  • Q: Is a list of exceptions easy to maintain?

  • A: No

  • Q: Is creating a library to deal with these usecases overkill?

  • A: Maybe. But is too late now.

Introducing ignore files

The Git project and their users had the same issue. They solved this with the .gitignore(pathspec) file.

This library offers some helper classes and functions to apply this to your project.

Release files for fpf 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fpf 0.1.4
File Size Uploaded
fpf-0.1.4.tar.gz 9.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fpf 0.1.4
File Interpreter ABI Platform
fpf-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 16.4 kB

Release files / fpf-0.1.4.tar.gz

Download URL fpf-0.1.4.tar.gz
Size 9.0 kB
Tags Source
SHA-256 checksum
How to use checksums
219f9b2015c2aba7a93ec6fc59bad478874ee0f9962bb4dd6126a8a5ed396c7b
BLAKE2b-256 checksum
How to use checksums
ee020f7cf201d0a06751894ed60acc326fd552d73fc9d24856881425c85780b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.10.0b2+

Release files / fpf-0.1.4-py3-none-any.whl

Download URL fpf-0.1.4-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b36550b1bc6cb1ee6c9a58109ef6bc0dd0bf5b7e48506e83b87e5acdfb686033
BLAKE2b-256 checksum
How to use checksums
d303ad2f7ced0c16a96057e515f0c4e117007b1887aafea3295aef25bb29360b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.10.0b2+

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 release files

0.1.3

2 release files

0.1.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page