mltk.utils.path

File path utilities

See the source code on Github: mltk/utils/path.py

Functions

add_user_setting(name, value)

Add an entry to the user settings

clean_directory(path)

Remove all files within directory and subdirectories

copy_directory(src, dst[, exclude_dirs])

Recursively copy a directory.

create_dir(path)

Create the given path's directories

create_tempdir([subdir])

Create a temporary directory as <temp dir>/<username>/mltk

create_user_dir([suffix, base_dir])

Create a user directory

extension(path)

Return the extension of the given path

file_is_in_use(file_path)

Return if the file is currently opened

file_is_newer(source_path, other_path)

fullpath(path[, cwd])

Return the full, normalized path of the given path

get_actual_path(path)

Return the file path as it actually appears on the FS (including upper/lower case) Return None if the path doesn't exist

get_user_setting(name[, default])

Return the value of a user setting if it exists

has_filename(path)

Return if the given path has a filename with an extension or is only a directory path

pushd(new_dir)

Change to the given directory, execute, and return to the previous directory

recursive_listdir(base_dir[, followlinks, ...])

Return list of all files recursively found in base_dir

remove_directory(path)

Remove the directory at the given path

set_file_last_modified(file_path[, dt])

walk_with_depth(base_dir[, depth, followlinks])

Walk a directory with a max depth.

fullpath(path, cwd=None)[source]

Return the full, normalized path of the given path

Return type:

str

Parameters:
  • path (str) –

  • cwd (str) –

get_actual_path(path)[source]

Return the file path as it actually appears on the FS (including upper/lower case) Return None if the path doesn’t exist

Parameters:

path (str) –

extension(path)[source]

Return the extension of the given path

Return type:

str

Parameters:

path (str) –

has_filename(path)[source]

Return if the given path has a filename with an extension or is only a directory path

Return type:

bool

Parameters:

path (str) –

create_dir(path)[source]

Create the given path’s directories

Return type:

str

Parameters:

path (str) –

create_tempdir(subdir='')[source]

Create a temporary directory as <temp dir>/<username>/mltk

Return type:

str

create_user_dir(suffix='', base_dir=None)[source]

Create a user directory

This creates the directory in one of the following base directories based on availability: - base_dir argument - OS environment variable: MLTK_CACHE_DIR - ~/.mltk - <user temp dir>/<username>/mltk

Parameters:
  • suffix (str) – Optional suffix to append to the base directory

  • base_dir (str) – Optional base directory, default to MLTK_CACHE_DIR, ~/.mltk, or <user temp dir>/<user name>/mltk if omitted

Return type:

str

Returns:

path to created directory

get_user_setting(name, default=None)[source]

Return the value of a user setting if it exists

User settings are defined in the file:

  • Environment variable: MLTK_USER_SETTINGS_PATH

  • OR <user home>/.mltk/user_settings.yaml

User settings include:

  • model_paths: list of directories to search for MLTK models

  • commander: Simplicity Commander options device: Device code serial_number: Adapter serial number ip_address: Adapter IP address

See settings_file

Parameters:

name (str) –

add_user_setting(name, value)[source]

Add an entry to the user settings

User settings are defined in the file: <user home>/.mltk/user_settings.yaml

Parameters:
  • name (str) –

  • value (object) –

remove_directory(path)[source]

Remove the directory at the given path

This will remove non-empty directories and retry a few times if necessary

Parameters:

path (str) –

clean_directory(path)[source]

Remove all files within directory and subdirectories

Parameters:

path (str) –

copy_directory(src, dst, exclude_dirs=None)[source]

Recursively copy a directory. Only copy files that are new or out-dated

set_file_last_modified(file_path, dt=None)[source]
Parameters:
  • file_path (str) –

  • dt (datetime) –

file_is_newer(source_path, other_path)[source]
Parameters:
  • source_path (str) –

  • other_path (str) –

file_is_in_use(file_path)[source]

Return if the file is currently opened

Return type:

bool

Parameters:

file_path (str) –

recursive_listdir(base_dir, followlinks=True, regex=None, return_relative_paths=False)[source]

Return list of all files recursively found in base_dir

Parameters:
  • base_dir (str) – The base directory to recursively search

  • followlinks – IF true then follow symbolic links

  • regex (Union[str, Pattern, Callable[[str], bool]]) – Optional regex of file paths to INCLUDE in the returned list This can either be a string, re.Pattern, or a callback function If return_relative_paths=False then the tested path is the absolute path with forward slashes If return_relative_paths=True then the tested path is the path relative to the base_dir with forward slashes If a callback function is given, if the function returns True then the path is INCLUDED, else it is excluded

  • return_relative_paths (bool) – If true then return paths relative to the base_dir, else return absolute paths

Return type:

List[str]

Returns:

List of file paths with forward slashes for directory delimiters

walk_with_depth(base_dir, depth=1, followlinks=True)[source]

Walk a directory with a max depth.

This is similar to os.walk except it has an optional maximum directory depth that it will walk

Return type:

Iterator[Tuple[str, List[str], List[str]]]

Parameters:

base_dir (str) –

pushd(new_dir)[source]

Change to the given directory, execute, and return to the previous directory

Example:

print(f’Old path: {os.getcwd()}’) with pushd(‘some/path’)

print(f’New path: {os.getcwd()}’)

print(f’Old path: {os.getcwd()}’)

Parameters:

new_dir (str) –