files
Filesystem helpers for listing, filtering, and creating temporary paths.
Examples:
>>> from ures.files import get_temp_folder
>>> import os
>>> os.path.isdir(get_temp_folder())
True
get_file_paths
get_file_paths(directory: str) -> list[str]
Retrieve all file paths within the specified directory and its subdirectories.
This function walks through the given directory recursively and returns a list of absolute file paths for every file found.
Parameters:
-
directory(str) –The directory path to search in.
Returns:
Examples:
>>> paths = get_file_paths("/path/to/directory")
>>> isinstance(paths, list)
True
Source code in ures/files.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | |
filter_files
filter_files(part_file_name: str, directory: str, fuzz: bool = True) -> list[str]
Retrieve file paths in the specified directory that match a given file name pattern.
This function filters file paths within a directory by checking if the file name contains the provided keyword. When 'fuzz' is True, it returns all files that contain the keyword; otherwise, it returns only the files whose name exactly matches the keyword.
Parameters:
-
part_file_name(str) –The substring or keyword to search for in file names.
-
directory(str) –The directory path to search in.
-
fuzz(bool, default:True) –If True, returns all files containing the keyword. If False, returns only files with an exact name match. Defaults to True.
Returns:
Examples:
>>> matched = filter_files("exam", "/tmp/test", fuzz=True)
>>> isinstance(matched, list)
True
Source code in ures/files.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
list_directories
list_directories(path: str) -> list[str] | None
List all subdirectories in the specified path.
This function checks whether the given path exists and then returns a list of names for each entry in the path that is a directory.
Parameters:
-
path(str) –The path where directories should be listed.
Returns:
Examples:
>>> dirs = list_directories("/tmp")
>>> isinstance(dirs, list) or dirs is None
True
Source code in ures/files.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |
get_temp_folder
get_temp_folder() -> str
Obtain the path of a new temporary folder.
This function creates a temporary directory using Python's tempfile module and returns its path.
Returns:
-
str(str) –The path to the newly created temporary folder.
Examples:
>>> temp_folder = get_temp_folder()
>>> os.path.isdir(temp_folder)
True
Source code in ures/files.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | |
get_temp_dir_with_specific_path
get_temp_dir_with_specific_path(*args) -> str
Create (if necessary) and return a temporary directory with a specified subpath.
This function constructs a path within the system's temporary directory using the provided arguments. If the directory does not already exist, it is created.
Parameters:
-
*args(str, default:()) –Subdirectory path components under the system temp directory.
Returns:
-
str(str) –The full path of the temporary directory created or existing.
Examples:
>>> temp_dir = get_temp_dir_with_specific_path("myapp", "cache")
>>> temp_dir.endswith(os.path.join("myapp", "cache"))
True
Source code in ures/files.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |