11
11
# ------------------------------------------------------------------------- #
12
12
# find,file_pattern:string,root:string
13
13
# ------------------------------------------------------------------------- #
14
+ # mkdir,folder:string
15
+ # ------------------------------------------------------------------------- #
14
16
15
17
import sys
16
18
import os
17
19
import re
20
+ import shutil
18
21
19
22
20
23
def file_operation (file_pattern : str , root : str , target : str , callback ):
21
24
for (dirpath , dirnames , filenames ) in os .walk (root ):
22
25
for filename in filenames :
23
26
path = '%s%s%s' % (dirpath , os .path .sep , filename )
24
27
if re .search (file_pattern , path ):
28
+ target = '%s%s%s' % (target , '' if target .endswith (
29
+ os .path .sep ) else os .path .sep , filename )
25
30
callback (path , target )
26
31
27
32
@@ -34,6 +39,15 @@ def callback_of_removing(path: str, target: str):
34
39
os .remove (path )
35
40
36
41
42
+ def callback_of_copying (path : str , target : str ):
43
+ print ('copy file %s to %s' % (path , target ))
44
+ shutil .copyfile (path , target )
45
+
46
+ def callback_of_moving (path : str , target : str ):
47
+ print ('move file %s to %s' % (path , target ))
48
+ shutil .move (path , target )
49
+
50
+
37
51
# print(sys.argv)
38
52
for arg in sys .argv [1 :]:
39
53
cmd_arg = str .split (arg , ',' )[1 :]
@@ -55,3 +69,9 @@ def callback_of_removing(path: str, target: str):
55
69
file_operation (file_pattern , root , target , callback_of_removing )
56
70
if str .startswith (arg , 'find' ):
57
71
file_operation (file_pattern , root , target , callback_of_finding )
72
+ if str .startswith (arg , 'cp' ):
73
+ file_operation (file_pattern , root , target , callback_of_copying )
74
+ if str .startswith (arg , 'mv' ):
75
+ file_operation (file_pattern , root , target , callback_of_moving )
76
+ if str .startswith (arg , 'mkdir' ):
77
+ os .makedirs (file_pattern )
0 commit comments