-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathgenerate_report_on__list_of_files.py
87 lines (67 loc) · 2.38 KB
/
generate_report_on__list_of_files.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "ipetrash"
import os
import time
from os.path import join
from human_byte_size import sizeof_fmt
def get_files_info(dir_name):
for root, dirs, files in os.walk(dir_name):
for file_name in files:
abs_file_name = join(root, file_name)
yield abs_file_name, os.stat(abs_file_name)
def get_date_as_string(dt):
return time.strftime("%H:%M:%S %m.%d.%y", time.gmtime(dt))
if __name__ == "__main__":
dir_name = r"D:\Program Files (x86)\Microsoft Office"
# Сортировка по размеру
files_sorted_by_size = sorted(
get_files_info(dir_name), reverse=True, key=lambda x: x[1].st_size
)
# # Без сортировки
# files_sorted_by_size = get_files_info(dir_name)
# Сохраняем в HTML файл
with open("result.html", "w", encoding="utf-8") as f:
f.write(
"""
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'/>
<style>
/* Добавление сетки таблицы */
table {
border-collapse: collapse; /* Убираем двойные линии между ячейками */
}
td, th {
padding: 3px; /* Поля вокруг содержимого таблицы */
border: 1px solid black; /* Параметры рамки */
}
</style>
</head>
<body>
<table>
"""
)
f.write(f"<capture>{dir_name}</capture>")
f.write(
"<tr><td>{}</td><td>{}</td><td>{}</td></tr>".format(
"FILE NAME", "SIZE", "LAST MODIFICATION"
)
)
for file_name, file_stat in files_sorted_by_size:
f.write("<tr>")
f.write(
"<td>{}</td><td>{}</td><td>{}</td>".format(
f'<a href="file://{file_name}">{file_name}</a>',
sizeof_fmt(file_stat.st_size),
get_date_as_string(file_stat.st_mtime),
)
)
f.write("</tr>")
f.write(
"""
</table>
</body>
</html>
"""
)