Skip to main content

Filter Paths in Python the easy way

Project description

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.


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
│           ├──
│           ├──
│           ├──
│           └──
├── fpf
│   ├──
│   ├──
│   ├──
│   └──
├── setup.cfg
├── tests
│  ├──
│  └──
├── .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 ):
  • 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.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fpf-0.1.4.tar.gz (9.0 kB view hashes)

Uploaded source

Built Distribution

fpf-0.1.4-py3-none-any.whl (7.4 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page