mltk.utils.path¶
File path utilities
See the source code on Github: mltk/utils/path.py
Functions
|
Add an entry to the user settings |
|
Remove all files within directory and subdirectories |
|
Recursively copy a directory. |
|
Create the given path's directories |
|
Create a temporary directory as <temp dir>/<username>/mltk |
|
Create a user directory |
|
Return the extension of the given path |
|
Return if the file is currently opened |
|
|
|
Return the full, normalized path of the given path |
|
Return the file path as it actually appears on the FS (including upper/lower case) Return None if the path doesn't exist |
|
Return the value of a user setting if it exists |
|
Return if the given path has a filename with an extension or is only a directory path |
|
Change to the given directory, execute, and return to the previous directory |
|
Return list of all files recursively found in base_dir |
|
Remove the directory at the given path |
|
|
|
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 directorybase_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
- 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 searchfollowlinks – 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 excludedreturn_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