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”Description
Section titled “Description”Remove unnecessary path.exists() check.
Before
Section titled “Before”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()Explanation
Section titled “Explanation”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 callpathlib.Path.exists()before.