I would like my program to write every warning in a .txt file. Is there any way to do this without using catch_warnings?
-
Yes it is possible. Open/create a file and then write to it. – Terry Jul 16 '20 at 09:47
-
2@Terry That seems pretty unrelated to Python warnings. – Arthur Tacca Jul 16 '20 at 09:53
-
I added the "compiler-warnings" tag. Although Python obviously doesn't have a compiler, its built-in warnings are basically analgous to compiler warnings in other langauges, so it seemed most appropriate. – Arthur Tacca Jul 16 '20 at 09:54
-
1@Tomerikoo They are talking about [Python warnings](https://docs.python.org/3/library/warnings.html) which are different from exceptions. – Arthur Tacca Jul 16 '20 at 10:06
-
@ArthurTacca oh I see. That was not very clear from the question (I am not familiar with that module...). Thanks. In that case it seems your answer solves this – Tomerikoo Jul 16 '20 at 10:09
-
@Tomerikoo Can you vote to reopen? The two linked questions are totally unrelated. – Arthur Tacca Jul 16 '20 at 10:10
-
@ArthurTacca I did as those dups apparently are not relevant although I would leave the question closed as unclear... +1 to your answer anyway – Tomerikoo Jul 16 '20 at 10:13
-
But I have another answer to add that is substantially different, and I think would be useful to people searching for this in future. It's perfectly clear if you know what Python warnings are. I think almost all StackOverflow questions wouldn't make sense if you don't know anything about the domain. – Arthur Tacca Jul 16 '20 at 10:16
-
@ArthurTacca *Perfectly* clear is a bit of an overstatement... Saying *I would like my program to write every warning* is very general. An opener saying *I am using the warnings module* with a link will be perfectly clear. Anyway, I don't think there is a reason for 2 answers. Just edit it in – Tomerikoo Jul 16 '20 at 11:33
1 Answers
One option is to use the built-in Python logging. There is a lot of information on the internet about how to use the Python logging system, especially in the Python documentation for it (e.g. see the Logging HOWTO). But the simplest way to turn on logging to a file is with the basicConfig()
function, like this:
import logging
logging.basicConfig(filename="myfile.txt",level=logging.DEBUG)
Now you can enable logging of warnings with the captureWarnings()
function:
logging.captureWarnings(True)
As a bonus, you can now also log your own messages:
logging.info("My own message")
An alternative is to replace the warning handler yourself. This is slightly more work but it is a bit more focused on just warnings. The warning
module documentation for showwarning()
says:
You may replace this function with any callable by assigning to
warnings.showwarning
.
So you could define your own function with the same parameter list, and assign it to that variable:
warning_file = open("warnings.txt", "w")
def mywarning(message, category, filename, lineno, file=None, line=None):
warning_file.write(warnings.formatwarning(message, category, filename, lineno, file, line))
warnings.showwarning = mywarning
Note that I've opened warning_file
outside the function so it will be opened once when your Python script starts. I also used the formatwarning()
function so that the output is the same format as the warnings
module usually outputs.

- 8,833
- 2
- 31
- 49