問題背景:
win10
python xxx.py > c:test.txt
上面這句只能把信息輸出到test.txt,但是控制台看不到信息
#######################################################################
要想在控制台和test.txt同時得到信息,如下操作:
官網源碼:
import logging
logging.basicConfig(filename='log_examp.log',level=logging.DEBUG)#level=logging.DEBUG定義日志級別
logging.debug('This message should go to the log file')#輸出級別level=logging.DEBUG
logging.info('So should this')#輸出級別level=logging.INFO
logging.warning('And this, too')#輸出級別level=logging.WARNING
實戰項目部分代碼:
1 def a(self): 2 #with open("info.csv","r") as csvfile: 3 with open("產品庫模板(PLT導入)20161205_01.csv","r") as csvfile: 4 #讀取csv文件,返回的是迭代類型 5 self.read = csv.reader(csvfile) 6 for self.i in self.read: 7 if self.i[8] =="" or self.i[8]=="9" or self.i[8]=='項目編號': 8 continue 9 else : 10 try: 11 self.b(self.i) 12 except Exception as e: 13 print(e) 14 self.logging.info(e) 15 16 finally: 17 print(self.i[8]) 18 self.logging.info(self.i[8])