Skip to content
This repository was archived by the owner on May 20, 2022. It is now read-only.

Commit b764aed

Browse files
committed
fix: optimize rm
1 parent bea8d70 commit b764aed

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

fops.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,22 @@
33
# run: python fops.py cmds
44

55
# -------------------------------------------- #
6-
# rm:cmd,file_pattern:string
6+
# rm:cmd,file_pattern:string[,root:string]
77
# -------------------------------------------- #
8-
# mv:cmd,file_pattern:string,target:string
8+
# mv:cmd,file_pattern:string[,root:string]
99
# -------------------------------------------- #
10-
# cp:cmd,file_pattern:string,target:string
10+
# cp:cmd,file_pattern:string[,root:string]
1111
# -------------------------------------------- #
12-
# find,root:string,file_pattern:string
12+
# find,file_pattern:string[,root:string]
1313
# -------------------------------------------- #
1414

1515
import sys
1616
import os
1717
import re
1818

1919

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):
2622
for filename in filenames:
2723
path = '%s%s%s' % (dirpath, os.path.sep, filename)
2824
if re.search(file_pattern, path):
@@ -34,5 +30,14 @@ def remove_by_file_pattern(args: list):
3430
for arg in sys.argv[1:]:
3531
cmd_arg = str.split(arg, ',')[1:]
3632

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+
3742
if str.startswith(arg, 'rm'):
38-
remove_by_file_pattern(cmd_arg)
43+
remove_by(file_pattern, root)

0 commit comments

Comments
 (0)