我是靠谱客的博主 幽默日记本,这篇文章主要介绍python将print输出的信息保留到日志文件中,现在分享给大家,希望可以做个参考。

复制代码
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
import sys import os import sys import io import datetime def create_detail_day(): ''' :return: ''' # 年-月-日 # daytime = datetime.datetime.now().strftime('day'+'%Y-%m-%d') # 年_月_日 daytime = datetime.datetime.now().strftime('day'+'%Y_%m_%d') # 时:分:秒 # hourtime = datetime.datetime.now().strftime("%H:%M:%S") # hourtime = datetime.datetime.now().strftime('time' + "%H_%M_%S") detail_time = daytime # print(daytime + "-" + hourtime) # detail_time = daytime + "__" + hourtime return detail_time def make_print_to_file(path='./'): ''' example: use make_print_to_file() , and the all the information of funtion print , will be write in to a log file :param path: the path to save print information :return: ''' class Logger(object): def __init__(self, filename="Default.log", path="./"): sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') self.terminal = sys.stdout self.log = open(os.path.join(path, filename), "a", encoding='utf8') def write(self, message): self.terminal.write(message) self.log.write(message) def flush(self): pass sys.stdout = Logger(create_detail_day() + '.log', path=path) print(create_detail_time().center(60,'*')) if __name__ == '__main__': make_print_to_file(path="/home/log/") print('explanation'.center(80, '*')) info1 = '从大到小排序' info2 = ' sort the form large to small' print(info1) print(info2) print('END: explanation'.center(80, '*'))

最后

以上就是幽默日记本最近收集整理的关于python将print输出的信息保留到日志文件中的全部内容,更多相关python将print输出内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(70)

评论列表共有 0 条评论

立即
投稿
返回
顶部