-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopyFile.py
More file actions
67 lines (60 loc) · 1.86 KB
/
copyFile.py
File metadata and controls
67 lines (60 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import os.path
import shutil
import re
from progress.bar import Bar
# 用于存放后缀名匹配的文件
greplist = []
def printdir(path, depth, pattern):
'''
遍历输出目录文件同时匹配后缀名
'''
for item in os.listdir(path):
print("| " * depth + item)
# 匹配文件后缀文件名
match = pattern.match(item)
if match:
greplist.append(path + '/' + item)
newitem = path + '/' + item
if os.path.isdir(newitem):
printdir(newitem, depth + 1, pattern)
def copy_file(greplist, suffix = "copy"):
'''
复制抓取匹配到的文件
'''
if 0 == len(greplist):
print("No Match File!")
return
# 目标文件夹
copytodir = os.path.curdir + '/' + suffix
if not os.path.exists(copytodir):
os.mkdir(copytodir)
os.chdir(copytodir)
# 打印进度条
length = len(greplist)
bar = Bar('Copying...', max = length)
for item in greplist:
# 复制匹配到的文件
try:
# 当前目录文件不复制
if item.startswith('.'):
print("File %s is existed!" % item)
else:
shutil.copy(item, os.path.curdir)
except PermissionError:
# 没有复制权限
print("Don't have permission to copy the file ", item)
except FileNotFoundError:
# 找不到文件
print("Can't found file ", item)
bar.next()
bar.finish()
if __name__ == '__main__':
path = input("Enter path:\n")
if path == "":
path = "."
suffix = input("Enter suffix(eg: txt):\n")
pattern = re.compile(".*\."+suffix+"$")
print("Directory scan of ", path)
printdir(path, 0, pattern)
copy_file(greplist, suffix)