mypythontools.paths package

Some functions around paths. You can find here find_path() to find some path efficiently in some folder, excluding some other inner folders (like venv, node_modules etc.). There is also function to get desktop path in posix way.

There is a wsl_path class that was just copy pasted from https://github.com/psychonaute/wsl-pathlib/blob/master/wsl_pathlib/path.py to remove unnecessary requirements.

class mypythontools.paths.WslPath(*args, **kwargs)[source]

Bases: PosixPath

This is just copy pasted from

https://github.com/psychonaute/wsl-pathlib/blob/master/wsl_pathlib/path.py

Extend pathlib.Path by addding the properties wsl_path and win_path. depending on the plateform base class either pathlib’s PosixPath or WindowsPath. Path objects are instanciated with path representation matching the user’s OS. Both _wsl_path and _win_path properties are lazily converted for their getters, the first time they are requested.

property win_path: str

Initialise WslPath properties and return lazy loaded value. If the Path object had been created by other mean than a direct instanciation, ie: p2 = p1 / “add_on”, then initializes the WslPath properties now. Then builds wsl_path if it was not yet being build. Then returns the string representation of it.

property wsl_path: str

Initialise WslPath properties and return lazy loaded value. If the Path object had been created by other mean than a direct instanciation, ie: p2 = p1 / “add_on”, then initialize the WslPath properties now. Then builds WindowsPath the win_path if it was not yet being build. Then returns the string representation of it.

mypythontools.paths.find_path(name: str, folder: PathLike | None = None, exclude_names: Sequence[str] = ('node_modules', 'build', 'dist', 'venv'), exclude_paths: Sequence[PathLike] = (), levels: int = 5) Path[source]

Search for file or folder in defined folder (cwd() by default) and return it’s path.

Parameters
  • name (str) – Name of folder or file that should be found. If using file, use it with extension e.g. “app.py”.

  • folder (PathLike | None, optional) – Where to search. If None, then root is used (cwd by default). Defaults to None.

  • exclude_names (Sequence[str], optional) – List or tuple of ignored names. If this name is whenever in path, it will be ignored. Defaults to (‘node_modules’, ‘build’, ‘dist’, ‘venv’).

  • exclude_paths (Sequence[PathLike], optional) – List or tuple of ignored paths. If defined path is subpath of found file, it will be ignored. If relative, it has to be from cwd. Defaults to ().

  • levels (str, optional) – Recursive number of analyzed folders. Defaults to 5.

Returns

Found path.

Return type

Path

Raises

FileNotFoundError – If file is not found.

Example

>>> path = find_path("README.md", exclude_names=['venv'])
>>> path.exists()
True
mypythontools.paths.get_desktop_path() Path[source]

Get desktop path.

Returns

Return pathlib Path object. If you want string, use .as_posix()

Return type

Path

Example

>>> desktop_path = get_desktop_path()
>>> desktop_path.exists()
True
mypythontools.paths.isFolderEmpty(path: Union[Path, str]) bool[source]

Check whether folder is empty.

Parameters

path (PathLike) – Path to folder

Raises

RuntimeError – If there is no folder on path.

Returns

True or False

Return type

bool

Example

>>> from pathlib import Path
>>> from shutil import rmtree
>>> test_path = Path("isFolderEmptyFolder")
>>> test_path.mkdir()
>>> isFolderEmpty(test_path)
True
>>> isFolderEmpty(test_path.parent)
False
>>> rmtree("isFolderEmptyFolder")
mypythontools.paths.is_path_free(path: Union[Path, str])[source]

Check whether path is available. It means that it doesn’t exists yet or its a folder, but it’s empty.

Parameters

path (PathLike) – Path to be verified.

Returns

True or False

Return type

bool

Example

>>> from pathlib import Path
>>> from shutil import rmtree
>>> is_path_free("non/existing/path")
True
>>> test_path = Path("isFolderEmptyFolder")
>>> test_path.mkdir()
>>> is_path_free(test_path)
True
>>> is_path_free(test_path.parent)
False
>>> rmtree("isFolderEmptyFolder")
mypythontools.paths.validate_path(path: PathLike, error_prefix: None | str = None, error_file_name: None | str = None) Path[source]

Convert to pathlib path, resolve to full path and check if exists.

Parameters
  • path (PathLike) – Validated path.

  • error_prefix (None | str) – Prefix for raised error if file nor folder found. Defaults to None.

  • () (error_file_name) – In raised error it’s the name of file or folder that should be found, so user understand what happened. Defaults to None.

Raises

FileNotFoundError – If file nor folder do not exists.

Returns

Pathlib Path object.

Return type

Path

Example

>>> from pathlib import Path
>>> existing_path = validate_path(Path.cwd())
>>> non_existing_path = validate_path("not_existing")
Traceback (most recent call last):
FileNotFoundError: ...

Submodules