66from automation_file .utils .logging .loggin_instance import file_automation_logger
77
88
9- def copy_file (file_path : str , target_path : str ) -> None :
9+ def copy_file (file_path : str , target_path : str ) -> bool :
10+ """
11+ :param file_path: which file we want to copy (str path)
12+ :param target_path: put copy file on target path
13+ :return: True if success else False
14+ """
1015 file_path = Path (file_path )
1116 if file_path .is_file () and file_path .exists ():
1217 try :
1318 shutil .copy2 (file_path , target_path )
1419 file_automation_logger .info (f"Copy file origin path: { file_path } , target path : { target_path } " )
20+ return True
1521 except shutil .Error as error :
1622 file_automation_logger .error (f"Copy file failed: { repr (error )} " )
1723 else :
1824 file_automation_logger .error (f"Copy file failed: { repr (FileNotExistsException )} " )
25+ return False
1926
2027
21- def copy_specify_extension_file (file_dir_path : str , target_extension : str , target_path : str ) -> None :
28+ def copy_specify_extension_file (file_dir_path : str , target_extension : str , target_path : str ) -> bool :
29+ """
30+ :param file_dir_path: which dir we want to search
31+ :param target_extension: Which extension do we search
32+ :param target_path: copy file to target path
33+ :return: True if success else False
34+ """
2235 file_dir_path = Path (file_dir_path )
2336 if file_dir_path .exists () and file_dir_path .is_dir ():
2437 for file in file_dir_path .glob (f"**/*.{ target_extension } " ):
@@ -27,12 +40,19 @@ def copy_specify_extension_file(file_dir_path: str, target_extension: str, targe
2740 f"Copy specify extension file on dir"
2841 f"origin dir path: { file_dir_path } , target extension: { target_extension } , "
2942 f"to target path { target_path } " )
43+ return True
3044 else :
3145 file_automation_logger .error (
3246 f"Copy specify extension file failed: { repr (FileNotExistsException )} " )
47+ return False
3348
3449
35- def copy_all_file_to_dir (dir_path : str , target_dir_path : str ) -> None :
50+ def copy_all_file_to_dir (dir_path : str , target_dir_path : str ) -> bool :
51+ """
52+ :param dir_path: copy all file on dir
53+ :param target_dir_path: put file to target dir
54+ :return: True if success else False
55+ """
3656 dir_path = Path (dir_path )
3757 target_dir_path = Path (target_dir_path )
3858 if dir_path .is_dir () and target_dir_path .is_dir ():
@@ -43,6 +63,7 @@ def copy_all_file_to_dir(dir_path: str, target_dir_path: str) -> None:
4363 f"origin dir: { dir_path } , "
4464 f"target dir: { target_dir_path } "
4565 )
66+ return True
4667 except shutil .Error as error :
4768 file_automation_logger .error (
4869 f"Copy all file to dir failed, "
@@ -52,9 +73,16 @@ def copy_all_file_to_dir(dir_path: str, target_dir_path: str) -> None:
5273 )
5374 else :
5475 print (repr (DirNotExistsException ), file = sys .stderr )
76+ return False
5577
5678
57- def rename_file (origin_file_path , target_name : str , file_extension = None ) -> None :
79+ def rename_file (origin_file_path , target_name : str , file_extension = None ) -> bool :
80+ """
81+ :param origin_file_path: which dir we want to search file
82+ :param target_name: rename file to target name
83+ :param file_extension: Which extension do we search
84+ :return: True if success else False
85+ """
5886 origin_file_path = Path (origin_file_path )
5987 if origin_file_path .exists () and origin_file_path .is_dir ():
6088 if file_extension is None :
@@ -68,6 +96,7 @@ def rename_file(origin_file_path, target_name: str, file_extension=None) -> None
6896 file_index = file_index + 1
6997 file_automation_logger .info (
7098 f"Renamed file: origin file path:{ origin_file_path } , with new name: { target_name } " )
99+ return True
71100 except Exception as error :
72101 file_automation_logger .error (
73102 f"Rename file failed, "
@@ -79,15 +108,25 @@ def rename_file(origin_file_path, target_name: str, file_extension=None) -> None
79108 else :
80109 file_automation_logger .error (
81110 f"Rename file failed, error: { repr (DirNotExistsException )} " )
111+ return False
82112
83113
84114def remove_file (file_path : str ) -> None :
115+ """
116+ :param file_path: which file we want to remove
117+ :return: None
118+ """
85119 file_path = Path (file_path )
86120 if file_path .exists () and file_path .is_file ():
87121 file_path .unlink (missing_ok = True )
88122 file_automation_logger .info (f"Remove file, file path: { file_path } " )
89123
90124
91125def create_file (file_path : str , content : str ) -> None :
126+ """
127+ :param file_path: create file on path
128+ :param content: what content will write to file
129+ :return: None
130+ """
92131 with open (file_path , "w+" ) as file :
93132 file .write (content )
0 commit comments