flake8-use-pathlib

0.3.0 · active · verified Wed Apr 15

flake8-use-pathlib is a plugin for the Flake8 linter that identifies patterns in Python code where functions from the `os.path` module (and other `os` functions) can be replaced by a more modern, object-oriented approach using the `pathlib` module. It helps developers write cleaner, more consistent, and cross-platform compatible code for file system operations. The current version is 0.3.0, released in August 2022, and it is actively maintained, with its rules also integrated into linters like Ruff.

Warnings

Install

Quickstart

After installation, `flake8-use-pathlib` automatically integrates with `flake8`. Simply run `flake8` on your Python files or project, and it will report issues where `os.path` functions can be replaced with `pathlib` equivalents, using 'PL' error codes.

# 1. Install flake8-use-pathlib (if you haven't already)
# pip install flake8 flake8-use-pathlib

# 2. Create a Python file, e.g., my_app.py, with some os.path usage:
# import os
#
# def get_absolute_path(filename):
#     return os.path.abspath(filename)
#
# def list_directory(path):
#     return os.listdir(path)
#
# print(get_absolute_path('file.txt'))
# print(list_directory('.'))

# 3. Run flake8 from your terminal in the directory containing my_app.py:
# flake8 my_app.py

# Expected output (or similar, depending on flake8 version and other plugins):
# my_app.py:4:12: PL100 os.path.abspath("filename") should be replaced by filename_path.resolve() (os-path-abspath)
# my_app.py:7:12: PL208 os.listdir(path) should be replaced by path_obj.iterdir() (os-listdir)

view raw JSON →