3
3
# run: python fops.py cmds
4
4
5
5
# -------------------------------------------- #
6
- # rm:cmd,file_pattern:string
6
+ # rm:cmd,file_pattern:string[,root:string]
7
7
# -------------------------------------------- #
8
- # mv:cmd,file_pattern:string,target :string
8
+ # mv:cmd,file_pattern:string[,root :string]
9
9
# -------------------------------------------- #
10
- # cp:cmd,file_pattern:string,target :string
10
+ # cp:cmd,file_pattern:string[,root :string]
11
11
# -------------------------------------------- #
12
- # find,root :string,file_pattern :string
12
+ # find,file_pattern :string[,root :string]
13
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 (args : list ):
21
- if len (args ) < 1 :
22
- print ('missing file pattern' )
23
- return
24
- file_pattern = args [0 ]
25
- for (dirpath , dirnames , filenames ) in os .walk ('.' ):
20
+ def remove_by (file_pattern : str , root : str ):
21
+ for (dirpath , dirnames , filenames ) in os .walk (root ):
26
22
for filename in filenames :
27
23
path = '%s%s%s' % (dirpath , os .path .sep , filename )
28
24
if re .search (file_pattern , path ):
@@ -34,5 +30,14 @@ def remove_by_file_pattern(args: list):
34
30
for arg in sys .argv [1 :]:
35
31
cmd_arg = str .split (arg , ',' )[1 :]
36
32
33
+ if len (cmd_arg ) < 1 :
34
+ print ('missing file pattern' )
35
+ exit ()
36
+ file_pattern = cmd_arg [0 ]
37
+ root = '.'
38
+
39
+ if len (cmd_arg ) > 1 :
40
+ root = cmd_arg [1 ]
41
+
37
42
if str .startswith (arg , 'rm' ):
38
- remove_by_file_pattern ( cmd_arg )
43
+ remove_by ( file_pattern , root )
0 commit comments