2
2
3
3
# run: python fops.py cmds
4
4
5
- # -------------------------------------------- #
6
- # rm:cmd,file_pattern:string[ ,root:string]
7
- # -------------------------------------------- #
8
- # mv:cmd,file_pattern:string[ ,root:string]
9
- # -------------------------------------------- #
10
- # cp:cmd,file_pattern:string[ ,root:string]
11
- # -------------------------------------------- #
12
- # find,file_pattern:string[ ,root:string]
13
- # -------------------------------------------- #
5
+ # ------------------------------------------------------------------------- #
6
+ # rm:cmd,file_pattern:string,root:string
7
+ # ------------------------------------------------------------------------- #
8
+ # mv:cmd,file_pattern:string,root:string,target:string
9
+ # ------------------------------------------------------------------------- #
10
+ # cp:cmd,file_pattern:string,root:string,target:string
11
+ # ------------------------------------------------------------------------- #
12
+ # find,file_pattern:string,root:string
13
+ # ------------------------------------------------------------------------- #
14
14
15
15
import sys
16
16
import os
17
17
import re
18
18
19
19
20
- def remove_by (file_pattern : str , root : str ):
20
+ def file_operation (file_pattern : str , root : str , target : str , callback ):
21
21
for (dirpath , dirnames , filenames ) in os .walk (root ):
22
22
for filename in filenames :
23
23
path = '%s%s%s' % (dirpath , os .path .sep , filename )
24
24
if re .search (file_pattern , path ):
25
- print ('remove file %s' % path )
26
- os .remove (path )
25
+ callback (path , target )
26
+
27
+
28
+ def callback_of_finding (path : str , target : str ):
29
+ print (path )
30
+
31
+
32
+ def callback_of_removing (path : str , target : str ):
33
+ print ('remove file %s' % path )
34
+ os .remove (path )
27
35
28
36
29
37
# print(sys.argv)
30
38
for arg in sys .argv [1 :]:
31
39
cmd_arg = str .split (arg , ',' )[1 :]
32
-
40
+ cmd_arg . sort
33
41
if len (cmd_arg ) < 1 :
34
42
print ('missing file pattern' )
35
43
exit ()
@@ -39,5 +47,11 @@ def remove_by(file_pattern: str, root: str):
39
47
if len (cmd_arg ) > 1 :
40
48
root = cmd_arg [1 ]
41
49
50
+ target = '.'
51
+ if len (cmd_arg ) > 2 :
52
+ target = cmd_arg [2 ]
53
+
42
54
if str .startswith (arg , 'rm' ):
43
- remove_by (file_pattern , root )
55
+ file_operation (file_pattern , root , target , callback_of_removing )
56
+ if str .startswith (arg , 'find' ):
57
+ file_operation (file_pattern , root , target , callback_of_finding )
0 commit comments