path
thkit.path
¶
Functions:
-
make_dir–Create a directory with a backup option.
-
make_dir_ask_backup–Make a directory and ask for backup if the directory already exists.
-
ask_yesnoback–Ask user for a yes/no/backup response.
-
ask_yesno–Ask user a yes/no question and return the normalized choice.
-
list_paths–List all files/folders in given directories and their subdirectories that match the given patterns.
-
collect_files–Collect files from a list of paths (files/folders). Will search files in folders and their subdirectories.
-
change_pathname–Change path names.
-
remove_files–Remove files from a given list of file paths.
-
remove_dirs–Remove a list of directories.
-
remove_files_in_paths–Remove files in the
fileslist in thepathslist. -
remove_dirs_in_paths–Remove directories in the
dirslist in thepathslist. -
copy_file–Copy a file/folder from the
source pathto thedestination path. It will create the destination directory if it does not exist. -
move_file–Move a file/folder from the source path to the destination path.
-
filter_dirs–Return directories containing
has_filesand none ofno_files.
make_dir(path: str | Path, backup: bool = True)
¶
Create a directory with a backup option.
make_dir_ask_backup(dir_path: str, logger: logging.Logger | None = None)
¶
Make a directory and ask for backup if the directory already exists.
ask_yesnoback(prompt: str) -> str
¶
Ask user for a yes/no/backup response.
ask_yesno(prompt: str) -> str
¶
Ask user a yes/no question and return the normalized choice.
list_paths(paths: str | list[str], patterns: list[str], recursive=True) -> list[str]
¶
List all files/folders in given directories and their subdirectories that match the given patterns.
Parameters:
-
paths–str | list[str] One or more root directories to search.
-
patterns–list[str] Glob patterns to match (e.g. ".txt", "data", "/", "**/.py").
-
recursive–bool, default=True If True, search recursively using rglob(). If False, search only the top-level directory using glob().
Returns:
Example:¶
folders = ["path1", "path2", "path3"]
patterns = ["*.ext1", "*.ext2", "something*.ext3", "*folder/"]
files = list_files_in_dirs(folders, patterns)
Notes:
- glob() does not list hidden files by default. To include hidden files, use glob(".", recursive=True).
- When use recursive=True, must include ** in the pattern to search subdirectories.
- glob("", recursive=True) will search all FILES & FOLDERS in the CURRENT directory.
- glob("/", recursive=True) will search all FOLDERS in the current CURRENT directory.
- glob("", recursive=True) will search all FILES & FOLDERS in the CURRENT & SUB subdirectories.
- glob("/", recursive=True) will search all FOLDERS in the current CURRENT & SUB subdirectories.
- "**/" is equivalent to "".
- "//" is equivalent to "*/".
- IMPORTANT: "/" will replicate the behavior of "**", then give unexpected results.
Revised
- Use
Path.rglob()is better than"**"strings - Hidden files are NOT matched unless explicitly included in a pattern (e.g. ".*").
- When
recursive=True, patterns are interpreted relative to each root path. You usually do NOT need to manually add "**". - Trailing "/" in patterns may be used to indicate directories.
collect_files(paths: list[str | Path] | str, patterns: list[str], recursive: bool = True) -> list[str]
¶
Collect files from a list of paths (files/folders). Will search files in folders and their subdirectories.
- Files are added directly.
- Directories are searched recursively using the given patterns.
- Glob patterns in paths are expanded as-is.
Parameters:
-
paths–list[str] | str The list of paths to collect files from.
-
patterns–list[str] The list of patterns to apply to the files. Each filter can be a file extension or a pattern.
-
recursive–bool, optional Whether to search directories recursively. Defaults to True.
Returns: list[str]: Sorted list of matching file paths (POSIX style).
change_pathname(paths: list[str], old_string: str, new_string: str, replace: bool = False) -> None
¶
remove_files(files: list[str]) -> None
¶
remove_dirs(dirs: list[str]) -> None
¶
remove_files_in_paths(files: list, paths: list) -> None
¶
Remove files in the files list in the paths list.
remove_dirs_in_paths(dirs: list, paths: list) -> None
¶
Remove directories in the dirs list in the paths list.
copy_file(src_path: str, dest_path: str)
¶
Copy a file/folder from the source path to the destination path. It will create the destination directory if it does not exist.
move_file(src_path: str, dest_path: str)
¶
Move a file/folder from the source path to the destination path.
filter_dirs(dirs: list[str], has_files: list[str] | None = None, no_files: list[str] | None = None) -> list[str]
¶
Return directories containing has_files and none of no_files.
Parameters:
-
dirs(list[str]) –List of directory paths to scan.
-
has_files(list[str] | None, default:None) –Files that must exist in the directory. Defaults to [].
-
no_files(list[str] | None, default:None) –Files that must not exist in the directory. Defaults to [].
Returns: