您好,登錄后才能下訂單哦!
具體代碼如下所示:
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輸出的信息保留到日志文件中,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。