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

Commit 64dca2c

Browse files
committed
feat: file move and mkdir
1 parent f0bd122 commit 64dca2c

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

fops.py

+20
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,22 @@
1111
# ------------------------------------------------------------------------- #
1212
# find,file_pattern:string,root:string
1313
# ------------------------------------------------------------------------- #
14+
# mkdir,folder:string
15+
# ------------------------------------------------------------------------- #
1416

1517
import sys
1618
import os
1719
import re
20+
import shutil
1821

1922

2023
def file_operation(file_pattern: str, root: str, target: str, callback):
2124
for (dirpath, dirnames, filenames) in os.walk(root):
2225
for filename in filenames:
2326
path = '%s%s%s' % (dirpath, os.path.sep, filename)
2427
if re.search(file_pattern, path):
28+
target = '%s%s%s' % (target, '' if target.endswith(
29+
os.path.sep) else os.path.sep, filename)
2530
callback(path, target)
2631

2732

@@ -34,6 +39,15 @@ def callback_of_removing(path: str, target: str):
3439
os.remove(path)
3540

3641

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+
3751
# print(sys.argv)
3852
for arg in sys.argv[1:]:
3953
cmd_arg = str.split(arg, ',')[1:]
@@ -55,3 +69,9 @@ def callback_of_removing(path: str, target: str):
5569
file_operation(file_pattern, root, target, callback_of_removing)
5670
if str.startswith(arg, 'find'):
5771
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

Comments
 (0)