path
thutil.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_yes_no
–Asks a yes/no/backup question and returns the response.
-
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
files
list in thepaths
list. -
remove_dirs_in_paths
–Remove directories in the
dirs
list in thepaths
list. -
copy_file
–Copy a file/folder from the source path to the destination path.
-
move_file
–Move a file/folder from the source path to the destination path.
-
scan_dirs
–Check if the folders contains and not contains some 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_yes_no(question: str) -> str
¶
Asks a yes/no/backup question and returns the response.
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.
move_file(src_path: str, dest_path: str)
¶
Move a file/folder from the source path to the destination path.
scan_dirs(dirs: list[str], with_files: list[str], without_files: list[str] = []) -> list[str]
¶
Check if the folders contains and not contains some files.
Parameters:
-
dirs
(list[str]
) –The paths of dirs to scan.
-
with_files
(list[str]
) –The files that should exist in the path.
-
without_files
(list[str]
, default:[]
) –The files that should not exist in the work_dir. Defaults to [].
Returns:
-
list[str]
–list[str]: The paths that meet the conditions.