Files Module
filter_files(part_file_name, directory, fuzz=True)
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:
| Name | Type | Description | Default |
|---|---|---|---|
part_file_name
|
str
|
The substring or keyword to search for in file names. |
required |
directory
|
str
|
The directory path to search in. |
required |
fuzz
|
bool
|
If True, returns all files containing the keyword. If False, returns only files with an exact name match. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: A list of file paths that match the search criteria. |
Example
Assuming "/tmp/test" contains files "example.txt" and "sample.txt"
filter_files("exam", "/tmp/test", fuzz=True) ['/tmp/test/example.txt']
Source code in ures/files.py
36 37 38 39 40 41 42 43 44 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 | |
get_file_paths(directory)
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:
| Name | Type | Description | Default |
|---|---|---|---|
directory
|
str
|
The directory path to search in. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: A list containing the absolute paths of all files found. |
Example
paths = get_file_paths("/path/to/directory") isinstance(paths, list) True
Source code in ures/files.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
get_temp_dir_with_specific_path(*args)
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:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Variable length arguments that specify the subdirectory path components. |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The full path of the temporary directory created or existing. |
Example
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
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
get_temp_folder()
Obtain the path of a new temporary folder.
This function creates a temporary directory using Python's tempfile module and returns its path.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The path to the newly created temporary folder. |
Example
temp_folder = get_temp_folder() os.path.isdir(temp_folder) True
Source code in ures/files.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
list_directories(path)
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:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The path where directories should be listed. |
required |
Returns:
| Type | Description |
|---|---|
list[str] | None
|
list[str] | None: A list of directory names if the path exists; otherwise, None. |
Example
dirs = list_directories("/tmp") isinstance(dirs, list) or dirs is None True
Source code in ures/files.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | |