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_user_action–Ask user for one of: yes / no / backup, and return 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, backup: bool = True)
¶
Create a directory with a backup option.
make_dir_ask_backup(dir_path: str)
¶
Make a directory and ask for backup if the directory already exists.
ask_user_action(question: str) -> str
¶
Ask user for one of: yes / no / backup, and return normalized choice.
list_paths(paths: 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 : list[str] The list of paths to search files/folders. patterns : list[str] The list of patterns to apply to the files. Each filter can be a file extension or a pattern.
Returns:¶
List[str]: A list of matching paths.
Example:¶
folders = ["path1", "path2", "path3"]
patterns = ["*.ext1", "*.ext2", "something*.ext3", "*folder/"]
files = list_files_in_dirs(folders, patterns)
Note:¶
- 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.
collect_files(paths: list[str], patterns: list[str]) -> list[str]
¶
Collect files from a list of paths (files/folders). Will search files in folders and their subdirectories.
Parameters¶
paths : list[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.
Returns:¶
List[str]: A list of paths matching files.
change_pathname(paths: list[str], old_string: str, new_string: str, replace: bool = False) -> None
¶
change path names
Parameters:
-
paths(list[str]) –paths to the files/dirs
-
old_string(str) –old string in path name
-
new_string(str) –new string in path name
-
replace(bool, default:False) –replace the old path name if the new one exists. Defaults to False.
remove_files(files: list[str]) -> None
¶
Remove files from a given list of file paths.
Parameters:
-
files(list[str]) –list of file paths
remove_dirs(dirs: list[str]) -> None
¶
Remove a list of directories.
Parameters:
-
dirs(list[str]) –list of directories to remove.
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, no_files: list[str] = 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], default:None) –Files that must exist in the directory. Defaults to [].
-
no_files(list[str], default:None) –Files that must not exist in the directory. Defaults to [].
Returns:
-
list[str]–List of directory paths meeting the conditions.