利用程序重定向输出为日志文件

 1 import sys
 2 import time
 3 import random as rd
 4 
 5 f = open("d:/a.txt",'w')
 6 old_fd = sys.stdout
 7 sys.stdout = f
 8 
 9 i = 1
10 while(1):
11     if i >1000:
12         break
13     i +=1
14     tm= time.localtime()
15     time.sleep(0.01)
16     # 2019-7-27  19:15:54 38.1961 35.4304 36.1544
17     print("{}-{}-{}  {}:{}:{} {:.4f} {:.4f} {:.4f}".format(tm.tm_year,tm.tm_mon,tm.tm_mday,
18                                               tm.tm_hour,tm.tm_min,tm.tm_sec,
19                                               rd.uniform(30,40),rd.uniform(30,40),rd.uniform(30,40)))
20 sys.stdout = old_fd
21 f.close()

猜你喜欢

转载自www.cnblogs.com/zach0812/p/11259353.html