Problem – To have a program that can issue warning messages (e.g., about deprecated features or usage problems).
Code #1 : Use the
The arguments to
warn() are a warning message along with a warning class, which is typically one of the following: UserWarning, DeprecationWarning, SyntaxWarning, RuntimeWarning, ResourceWarning, or FutureWarning.
The handling of warnings depends on how the interpreter is executed and other configuration. If Python with the
-W all option is run, the following output is obtained:
bash % python3 -W all example.py example.py:5: DeprecationWarning: logfile argument is deprecated warnings.warn('logfile argument is deprecated', DeprecationWarning)
Normally, warnings just produce output messages on standard error. To turn warnings into exceptions, use the
-W error option.
Code #2 :
bash % python3 -W error example.py Traceback (most recent call last): File "example.py", line 10, in
func(2, 3, logfile ='log.txt') File "example.py", line 5, in func warnings.warn('logfile argument is deprecated', DeprecationWarning) DeprecationWarning: logfile argument is deprecated bash %
Issuing a warning message is often a useful technique for maintaining software and assisting users with issues that don’t necessarily rise to the level of being a full-fledged exception.
Code #3 : Warning message generated by destroying a file without closing it.
__main__:1: ResourceWarning: unclosed file <_io.TextIOWrapper name='/etc/passwd' mode='r' encoding='UTF-8'>
- By default, not all warning messages appear. The -W option to Python can control the output of warning messages.
- -W all will output all warning messages, -W ignore ignores all warnings, and -W error turns warnings into exceptions.
- As an alternative, one can use the warnings.simplefilter() function to control output. An argument of always makes all warning messages appear, ignore ignores all warnings, and error turns warnings into exceptions.
- The warnings module provides a variety of more advanced configuration options related to the filtering and handling of warning messages.
- Python | Reraise the Last Exception and Issue Warning
- Python Tkinter - Message
- Create a Yes/No Message Box in Python using tkinter
- Python | Message Encode-Decode using Tkinter
- Python Program that Sends And Recieves Message from Client
- Python PyQt5 – How to change font and size of Status Bar Message
- Python | Prompt for Password at Runtime and Termination with Error Message
- PyQt5 – Set status bar message in window
- hmac - Keyed-Hashing for Message Authentication
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Index of Non-Zero elements in Python list
- Python | Convert list to Python array
- MySQL-Connector-Python module in Python
- Important differences between Python 2.x and Python 3.x with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.