Skip to content

Commit d28e39a

Browse files
committed
add path.is_path()
1 parent 79928ae commit d28e39a

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

pkg/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ namespaces = true
1515

1616
# ----------------------------------------- Project Metadata -------------------------------------
1717
[project]
18-
version = "0.2.1"
18+
version = "0.2.2"
1919
name = "FileEx"
2020
requires-python = ">=3.10"

pkg/src/fileex/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
"""FileEx"""
22

3-
from fileex import exception, directory, search
3+
from fileex import exception, directory, search, path
4+
5+
__all__ = [
6+
"exception",
7+
"directory",
8+
"search",
9+
"path",
10+
]

pkg/src/fileex/path.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from pathlib import Path
2+
from typing import Any
3+
4+
5+
def is_path(path: Any) -> bool:
6+
"""Check if the input is a path.
7+
8+
The input is considered a path if it is a `pathlib.Path` object
9+
or a string representing a path that exists on the filesystem.
10+
11+
Parameters
12+
----------
13+
path
14+
Path to check.
15+
"""
16+
if isinstance(path, Path):
17+
return True
18+
if not isinstance(path, str):
19+
return False
20+
path = Path(path).resolve()
21+
try:
22+
return path.exists()
23+
except OSError:
24+
# If the path cannot be resolved, it is not a valid path (e.g., too long)
25+
return False

0 commit comments

Comments
 (0)