dlpt.pth module

Functions for common path, file and directory manipulation.

class dlpt.pth.ChangeDir(path: str)

Bases: object

__init__(path: str)

Temporary change working directory of a block of code and revert to an original on exit.

Parameters:

path – path to an existing local directory/file that is temporary set as working directory. If file path is given, its directory is taken as new temporary working dir.

Example

>>> with dlpt.pth.ChangeDir("C:/somePath"):
        func("that does something in CWD")
dlpt.pth.check(path: str | None) str

Check if given path exists and return normalized path.

Note

Use standard os.path.exists() if you don’t want to raise exception.

Parameters:

path – path to check

Returns:

Normalized path (if valid) or raise exception.

dlpt.pth.resolve(path: str) str

Resolve path with pathlib module. This will (for existing files) fix any case mismatch, for example, drive letter.

Parameters:

path – abs path to resolve.

Returns:

Resolved path according to the OS.

dlpt.pth.copy_file(src_file_path: str, dst_dir_path: str, dst_file_name: str | None = None) str

Copy given file to a new location, while dstFile is removed prior copying. Any intermediate directories are created automatically.

Parameters:
  • src_file_path – path to a file to be copied.

  • dst_dir_path – absolute destination directory path.

  • dst_file_name – new destination file name. If None, original file name is used.

Returns:

A path to a copied file.

dlpt.pth.copy_dir(src_dir_path: str, dst_dir_path: str) str

Copy given directory to a new location while creating any intermediate directories. Prior copying, destination directory is removed.

Parameters:
  • src_dir_path – path to a file to be copied.

  • dst_dir_path – new destination path.

Returns:

A path to a copied directory.

dlpt.pth.remove_file(file_path: str, force_write_permissions: bool = True, retry: int = 3)

This function tries to remove file (FILE, not DIRECTORY) on a given path. Optionally, write permissions are set to a file.

Parameters:
  • file_path – path to a file.

  • force_write_permissions – if True, write permissions are set to a file so it can be removed.

  • retry – on failure, retry removal specified number of times.

dlpt.pth.remove_dir_tree(dir_path: str, force_write_permissions: bool = True, retry: int = 3)

Remove directory (DIRECTORY, not FILE) and all its content on a given path.

Parameters:
  • dir_path – path of a directory to remove.

  • force_write_permissions – if True, shutil.rmtree() error callback function is used to change permissions and retry.

  • retry – on failure, retry removal specified number of times. Must be > 0. Sometimes file are locked with other processes, or a race condition occurred.

dlpt.pth.clean_dir(dir_path: str, force_write_permissions: bool = True)

Delete all directory content (files, sub-directories) in a given directory, but not the root directory itself.

Parameters:
  • dir_path – path to a directory to clean all its content.

  • force_write_permissions – if True, write permissions are set to be able to delete files.

dlpt.pth.create_dir(dir_path: str)

Create directory (or directory tree) on a given specified path.

Parameters:

dir_path – absolute path of a directory to create.

dlpt.pth.create_clean_dir(dir_path: str)

Create new or clean existing directory on a given specified path. Path existence is checked with check() at the end.

Parameters:

dir_path – absolute path of a directory to create/clean.

dlpt.pth.remove_old_items(dir_path: str, days: int) List[str]

Remove items (files, directories) inside the given directory that were modified more than specified number of days ago.

Note

Modification time and current time can be the same when this function is called right after creation. Hence, decimal part (milliseconds) of current/modification timestamp is discarded.

Parameters:
  • dir_path – path to a directory with files/directories to remove.

  • days – number of days file/directory must be old to be removed (last modification time).

Returns:

A list of removed items.

dlpt.pth.with_fw_slashes(path: str) str

Convert path to use forward slashes.

Note

This function does not do os.path.normpath() so it is also usable for UNCs.

Parameters:

path – path to convert.

Returns:

A path with converted back slashes to forward slashes.

dlpt.pth.with_double_bw_slashes(path: str) str

Convert and return path to use double back slashes.

Parameters:

path – path to convert

Returns:

A converted path with double back slashes.

dlpt.pth.get_name(file_path: str, with_ext: bool = True) str

Return a file name from file path or raise exception.

Note

No file existence check is performed.

Parameters:
  • file_path – file path where file name will be fetched from.

  • with_ext – if False, extension is striped from file name.

Returns:

A file name with/without extension.

dlpt.pth.get_ext(file_path: str) str

Return file extension (with dot) from file path or raise exception.

Note

No file existence check is performed.

Parameters:

file_path – file path where file name will be fetched from.

Returns:

A file extension.

dlpt.pth.get_files_in_dir(dir_path: str, include_ext: List[str] | None = None, exclude_ext: List[str] | None = None) List[str]

Get a list of files in a given dir_path.

Note

Only one of include_ext or exclude_ext must be set, or exception is raised. Lower case extension strings are compared.

Parameters:
  • dir_path – path to a directory to scan.

  • include_ext – if set, only files with given extension(s) are returned.

  • exclude_ext – if set, files with given extension(s) are excluded from return list.

Returns:

List of matching files from dir_path`.

dlpt.pth.get_files_in_dir_tree(dir_tree_path: str, include_ext: List[str] | None = None, exclude_ext: List[str] | None = None) List[str]

Same as get_files_in_dir(), but scan through all files in all directories.

Note

Only one of include_ext or exclude_ext must be set, or exception is raised. Lower case extension strings are compared.

Parameters:
  • dir_tree_path – path to a directory tree to scan.

  • include_ext – if set, only files with given extension(s) are returned.

  • exclude_ext – if set, files with given extension(s) are excluded from return list.

Returns:

List of matching files from dir_path and all its sub-directories.

dlpt.pth.get_dirs_in_dir(dir_path: str, name_filter: str | None = None, case_insensitive: bool = True) List[str]

Get a list of directories in a given dir_path.

Parameters:
  • dir_path – path to a directory to scan.

  • name_filter – if set, directories that contain this string are returned, based on case_insensitive setting.

  • case_insensitive – if True, lower-cased name_filter string (if set) is checked in lower case directory name.

Returns:

List of matching directories from dir_path.

dlpt.pth.open_in_web_browser(url: str)

Open given address in a default web browser as a non-blocking subprocess.

Parameters:

url – web address to open.

dlpt.pth.open_with_default_app(file_path: str)

Open given file with OS default application as a non-blocking subprocess.

Parameters:

file_path – path to a file to open.