11import shutil
22from pathlib import Path
33
4+ # 匯入自訂例外與日誌工具
5+ # Import custom exception and logging utility
46from automation_file .utils .exception .exceptions import DirNotExistsException
57from automation_file .utils .logging .loggin_instance import file_automation_logger
68
79
810def copy_dir (dir_path : str , target_dir_path : str ) -> bool :
911 """
10- Copy dir to target path (path need as dir path)
11- :param dir_path: which dir do we want to copy (str path)
12- :param target_dir_path: copy dir to this path
13- :return: True if success else False
12+ 複製資料夾到目標路徑
13+ Copy directory to target path
14+ :param dir_path: 要複製的資料夾路徑 (str)
15+ Directory path to copy (str)
16+ :param target_dir_path: 複製到的目標資料夾路徑 (str)
17+ Target directory path (str)
18+ :return: 成功回傳 True,失敗回傳 False
19+ Return True if success, else False
1420 """
15- dir_path = Path (dir_path )
21+ dir_path = Path (dir_path ) # 轉換為 Path 物件 / Convert to Path object
1622 target_dir_path = Path (target_dir_path )
17- if dir_path .is_dir ():
23+ if dir_path .is_dir (): # 確認來源是否為資料夾 / Check if source is a directory
1824 try :
25+ # 複製整個資料夾,若目標已存在則允許覆蓋
26+ # Copy entire directory, allow overwrite if target exists
1927 shutil .copytree (dir_path , target_dir_path , dirs_exist_ok = True )
2028 file_automation_logger .info (f"Copy dir { dir_path } " )
2129 return True
2230 except shutil .Error as error :
31+ # 複製失敗時記錄錯誤
32+ # Log error if copy fails
2333 file_automation_logger .error (f"Copy dir { dir_path } failed: { repr (error )} " )
2434 else :
35+ # 若來源資料夾不存在,記錄錯誤
36+ # Log error if source directory does not exist
2537 file_automation_logger .error (f"Copy dir { dir_path } failed: { repr (DirNotExistsException )} " )
2638 return False
39+ return False
2740
2841
2942def remove_dir_tree (dir_path : str ) -> bool :
3043 """
31- :param dir_path: which dir do we want to remove (str path)
32- :return: True if success else False
44+ 刪除整個資料夾樹
45+ Remove entire directory tree
46+ :param dir_path: 要刪除的資料夾路徑 (str)
47+ Directory path to remove (str)
48+ :return: 成功回傳 True,失敗回傳 False
49+ Return True if success, else False
3350 """
3451 dir_path = Path (dir_path )
35- if dir_path .is_dir ():
52+ if dir_path .is_dir (): # 確認是否為資料夾 / Check if directory exists
3653 try :
37- shutil .rmtree (dir_path )
54+ shutil .rmtree (dir_path ) # 遞迴刪除資料夾 / Recursively delete directory
3855 file_automation_logger .info (f"Remove dir tree { dir_path } " )
3956 return True
4057 except shutil .Error as error :
4158 file_automation_logger .error (f"Remove dir tree { dir_path } error: { repr (error )} " )
4259 return False
60+ return False
4361
4462
4563def rename_dir (origin_dir_path , target_dir : str ) -> bool :
4664 """
47- :param origin_dir_path: which dir do we want to rename (str path)
48- :param target_dir: target name as str full path
49- :return: True if success else False
65+ 重新命名資料夾
66+ Rename directory
67+ :param origin_dir_path: 原始資料夾路徑 (str)
68+ Original directory path (str)
69+ :param target_dir: 新的完整路徑 (str)
70+ Target directory path (str)
71+ :return: 成功回傳 True,失敗回傳 False
72+ Return True if success, else False
5073 """
5174 origin_dir_path = Path (origin_dir_path )
5275 if origin_dir_path .exists () and origin_dir_path .is_dir ():
5376 try :
77+ # 使用 Path.rename 重新命名資料夾
78+ # Rename directory using Path.rename
5479 Path .rename (origin_dir_path , target_dir )
5580 file_automation_logger .info (
5681 f"Rename dir origin dir path: { origin_dir_path } , target dir path: { target_dir } " )
5782 return True
5883 except Exception as error :
84+ # 捕捉所有例外並記錄
85+ # Catch all exceptions and log
5986 file_automation_logger .error (
6087 f"Rename dir error: { repr (error )} , "
6188 f"Rename dir origin dir path: { origin_dir_path } , "
6289 f"target dir path: { target_dir } " )
6390 else :
91+ # 若來源資料夾不存在,記錄錯誤
92+ # Log error if source directory does not exist
6493 file_automation_logger .error (
6594 f"Rename dir error: { repr (DirNotExistsException )} , "
6695 f"Rename dir origin dir path: { origin_dir_path } , "
6796 f"target dir path: { target_dir } " )
6897 return False
98+ return False
6999
70100
71101def create_dir (dir_path : str ) -> None :
72102 """
73- :param dir_path: create dir on dir_path
103+ 建立資料夾
104+ Create directory
105+ :param dir_path: 要建立的資料夾路徑 (str)
106+ Directory path to create (str)
74107 :return: None
75108 """
76109 dir_path = Path (dir_path )
110+ # 若資料夾已存在則不會報錯
111+ # Create directory, no error if already exists
77112 dir_path .mkdir (exist_ok = True )
78- file_automation_logger .info (f"Create dir { dir_path } " )
113+ file_automation_logger .info (f"Create dir { dir_path } " )
0 commit comments