{"id":6642,"library":"flake8-use-pathlib","title":"flake8-use-pathlib","description":"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.","status":"active","version":"0.3.0","language":"en","source_language":"en","source_url":"https://github.com/PyCQA/flake8-use-pathlib","tags":["flake8","linter","pathlib","code-quality","os.path","refactoring"],"install":[{"cmd":"pip install flake8-use-pathlib","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"This is a plugin for Flake8 and requires Flake8 to be installed to function.","package":"flake8","optional":false}],"imports":[],"quickstart":{"code":"# 1. Install flake8-use-pathlib (if you haven't already)\n# pip install flake8 flake8-use-pathlib\n\n# 2. Create a Python file, e.g., my_app.py, with some os.path usage:\n# import os\n#\n# def get_absolute_path(filename):\n#     return os.path.abspath(filename)\n#\n# def list_directory(path):\n#     return os.listdir(path)\n#\n# print(get_absolute_path('file.txt'))\n# print(list_directory('.'))\n\n# 3. Run flake8 from your terminal in the directory containing my_app.py:\n# flake8 my_app.py\n\n# Expected output (or similar, depending on flake8 version and other plugins):\n# my_app.py:4:12: PL100 os.path.abspath(\"filename\") should be replaced by filename_path.resolve() (os-path-abspath)\n# my_app.py:7:12: PL208 os.listdir(path) should be replaced by path_obj.iterdir() (os-listdir)","lang":"bash","description":"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."},"warnings":[{"fix":"Use `pip install flake8-use-pathlib` instead of `pip install flake8-pathlib`.","message":"The package was previously named `flake8-pathlib`. Users should ensure they are installing `flake8-use-pathlib` as the former name is deprecated and no longer updated.","severity":"breaking","affected_versions":"<0.3.0 (for `flake8-pathlib` name)"},{"fix":"Pass `Path` objects directly to functions that accept them (e.g., `open(my_path)`). Only convert to `str` if a function strictly requires it and does not support `os.PathLike` objects.","message":"While `pathlib` generally improves code, converting `Path` objects to `str` too early, e.g., `open(str(my_path))`, negates `pathlib`'s benefits. Python's `open()` (since 3.6) and most modern libraries natively accept `Path` objects.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always review and test code after applying autofixes, especially those marked as potentially unsafe, to ensure functionality is preserved.","message":"Some autofixes derived from `flake8-use-pathlib` rules (e.g., for `os.path.expanduser` to `Path.expanduser`) are considered 'unsafe' because `os.path` and `pathlib.Path` methods can have subtly different behaviors (e.g., error handling) or return types.","severity":"gotcha","affected_versions":"All versions with autofix support (e.g., via Ruff)"},{"fix":"For highly performance-sensitive operations, consider benchmarking `pathlib` against `os` module functions. For most typical file operations, `pathlib`'s benefits in readability and maintainability outweigh minor performance differences.","message":"Using `pathlib` can sometimes be less performant than lower-level `os` module alternatives, especially on older Python versions or in performance-critical sections of code.","severity":"gotcha","affected_versions":"All versions, more pronounced on Python <3.8"},{"fix":"Ensure project-specific configurations are placed in `setup.cfg`, `tox.ini`, or `.flake8` within the project root, rather than relying on global `~/.flake8` settings for `flake8-use-pathlib` rules.","message":"Flake8's configuration file discovery changed in version 5.0.0. Configuration in `~/.flake8` is explicitly ignored for global settings.","severity":"gotcha","affected_versions":"Flake8 >=5.0.0"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}