忽略Python代碼中的提示信息(print)


忽略Python代碼中的提示信息

今天一直被 pymysql 庫的告警信息干擾,而本身所有代碼包括sql的運作是正常的

(py36) [root@VM_33_99_centos zabbix-externalscripts]# ./my_python_script.py
/app/zabbix-externalscripts/py36/lib/python3.6/site-packages/pymysql/cursors.py:170: Warning: (1292, "Incorrect datetime value: '0'")
  result = self._query(query)
0.448

想要輸出完成的 0.448,不要提示信息,怎么辦呢

搜了一下:python get print from function,無果,能獲取到stdout輸出沒錯,但這個stderr不行

想到這類輸出其實是蠻常見的,應該有解決方法吧

隨即搜索:python ignore print warning,got you~!

直達 stackoverflow 的快車

感覺以下兩種方法是比較常用/實用的

方法1. 在shell中添加執行參數

python -W ignore my_python_script.py

方法2. 使用warnings庫修改代碼結構

找到輸出warning的代碼段

import warnings

with warnings.catch_warnings():
	warnings.simplefilter('ignore')
	# ...你的代碼段

關鍵詞:python,warning,package,stderr,error


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM