This is my code that I wrote:
#usr/bin/python3
import warnings
def tt():
warnings.warn("123")
return 10
x = tt()
print(x)
It prints:
test.py:5: UserWarning: 123
warnings.warn("123")
10
I want it to only print:
test.py:5: UserWarning: 123
10
Without warnings.warn("123")
.
What should I do?
You can replace the function used to format the messages; for example:
def warning_on_one_line(message, category, filename, lineno, file=None, line=None):
return '%s:%s: %s:%s\n' % (filename, lineno, category.__name__, message)
warnings.formatwarning = warning_on_one_line
Python Module of the week article
import warnings
# import pdb
def format_Warning(message, category, filename, lineno, line=''):
return str(filename) + ':' + str(lineno) + ': ' + category.__name__ + ': ' +str(message) + '\n'
warnings.formatwarning = format_Warning
def tt():
warnings.warn("123")
return 10
x = tt()
print(x)
Hope it helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With