forked from iamseancheney/USTC-CS-Courses-Resource
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenZipFile.py
More file actions
70 lines (63 loc) · 2.25 KB
/
Copy pathgenZipFile.py
File metadata and controls
70 lines (63 loc) · 2.25 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
68
69
70
# coding: utf-8
import os
import shutil
from zipfile import ZipFile
from operator import or_
from functools import reduce
from config import WALKDIR
def checkZip(name):
'''check if this file should be added to the zip'''
li = [name.startswith('.') ,name.endswith('.zip'),name.lower()=='readme.md']
return not any(li)
def genZipFile(tar = WALKDIR,rewrite=False):
os.chdir(tar)
n = len(tar)
gen = os.walk(tar)
pwd = os.path.abspath('.')
for path, dirs, files in gen:
li = path.strip(os.sep).split(os.sep)
if reduce(or_,[i[0]=='.' for i in li],False) :continue # 可以用 any, all
ziplst = []
for i in files:
if i.endswith('个文件.zip'):
if rewrite:
os.remove(os.path.join(path,i))
else:break
elif checkZip(i):
ziplst .append(i)
else:
if len(ziplst)<3:continue
ziplst.sort()
tmp = os.path.abspath(path) \
.replace(pwd,'')\
.replace(os.sep,'-')
name = '{tmp}目录下的{length}个文件.zip'.format(tmp=tmp,length =len(ziplst))
zipName = os.path.join(path,name)
try:
with ZipFile(zipName,'w') as z:
os.chdir(path)
for i in ziplst: z.write(i)
except Exception as e:
print(e)
def checkBigFile():
big = os.path.join(WALKDIR,'.bigFile')
if not os.path.exists(big):
os.mkdir(big)
gen = os.walk(WALKDIR)
for path,dirs,files in gen:
li = path.strip(os.sep).split(os.sep)
if reduce(or_,[i[0]=='.' for i in li],False):continue
for file in files:
filePath = os.path.join(path,file)
size = os.path.getsize(filePath)
if size > (2**20)*100:
print('[BIG]: {} is bigger than 100mb'.format(filePath))
try:
shutil.move(filePath,big)
except Exception as e:
print(e)
os.remove(filePath)
if __name__ == '__main__':
#genZipFile(rewrite=True)
genZipFile(rewrite=False)
checkBigFile()