Skip to content

Remove-Redundant-Path-Exists

Documentation Index

Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt

Use this file to discover all available pages before exploring further.

Sourcery rule id: remove-redundant-path-exists

Section titled “Sourcery rule id: remove-redundant-path-exists”

Remove unnecessary path.exists() check.

from pathlib import Path
p = Path("/home/user/mydir")
if p.exists() and p.is_dir():
do_sth()
from pathlib import Path
p = Path("/home/user/mydir")
if p.is_dir():
do_sth()

The following functions already contain a check whether a path exists:

  • pathlib.Path.is_dir()
  • pathlib.Path.is_file()
  • pathlib.Path.is_symlink() There’s no need to explicitly call pathlib.Path.exists() before.