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

Commit f0bd122

Browse files
committed
feat: find file
1 parent b764aed commit f0bd122

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

fops.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,42 @@
22

33
# run: python fops.py cmds
44

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+
# ------------------------------------------------------------------------- #
1414

1515
import sys
1616
import os
1717
import re
1818

1919

20-
def remove_by(file_pattern: str, root: str):
20+
def file_operation(file_pattern: str, root: str, target: str, callback):
2121
for (dirpath, dirnames, filenames) in os.walk(root):
2222
for filename in filenames:
2323
path = '%s%s%s' % (dirpath, os.path.sep, filename)
2424
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)
2735

2836

2937
# print(sys.argv)
3038
for arg in sys.argv[1:]:
3139
cmd_arg = str.split(arg, ',')[1:]
32-
40+
cmd_arg.sort
3341
if len(cmd_arg) < 1:
3442
print('missing file pattern')
3543
exit()
@@ -39,5 +47,11 @@ def remove_by(file_pattern: str, root: str):
3947
if len(cmd_arg) > 1:
4048
root = cmd_arg[1]
4149

50+
target = '.'
51+
if len(cmd_arg) > 2:
52+
target = cmd_arg[2]
53+
4254
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

Comments
 (0)