forked from lwzswufe/enhancement
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml_generator.py
55 lines (47 loc) · 1.56 KB
/
html_generator.py
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
# author='lwz'
# coding:utf-8
# !/usr/bin/env python3
import os
def main(file_path="D:\\Code\\Code\\", rewrite=False):
flag = 0
for root, dirs, files in os.walk(file_path):
dir_name = root.split(sep='\\')[-1]
html_ = Html(dir_name, root, rewrite)
for fn in files:
suffix = fn[-4:]
if suffix == '.jpg' or suffix == '.png':
flag += 1
html_.add_img(fn)
html_.write()
del html_
class Html():
def __init__(self, name='test', path='D:\\', rewrite=False):
self.name = name
self.head = '<html>\n<body>\n'
self.tail = '</body>\n</html>\n'
self.context = ''
self.file_path = path
self.rewrite = rewrite
def add_img(self, fn):
self.context += '<img src="' + fn + '"/>\n'
def write(self):
if len(self.context) == 0:
try:
print(self.name, ' is empty')
except UnicodeEncodeError:
pass
return
else:
html_name = self.file_path + '\\' + self.name + ".html"
if not self.rewrite and os.path.exists(html_name):
return
try:
with open(html_name, 'w') as fp:
fp.write(self.head)
fp.write(self.context)
fp.write(self.tail)
except UnicodeEncodeError:
print('error', html_name)
print(self.name, ' write over')
if __name__ == '__main__':
main(file_path="D:\\360安全浏览器下载\\")