When I run my program, which is using:
nltk.download('wordnet')
from nltk.corpus import wordnet
I get the following output to my terminal:
[nltk_data] Downloading package wordnet to
[nltk_data] /Users/.../nltk_data...
[nltk_data] Package wordnet is already up-to-date!
My program relies on not having this information saved to the terminal and a resulting output file, so how can I prevent the above lines from occurring, or write it to sys.stderr
so it doesn't get included instead of it being through print
?
Use quiet=True
:
import nltk
nltk.download('wordnet', quiet=True)
A much better solution is suggested in this answer.
Old Answer:
According to the source code, nltk
downloader uses straightforward print()
calls to report progress. This means that there is no logger involved which you can control or pre-configure.
One of the options is to modify the sys.stdout
temporarily on the fly -
there is that redirect_stdout()
context manager in Python 3.4+:
from contextlib import redirect_stdout
import os
import nltk
from nltk.corpus import wordnet
with redirect_stdout(open(os.devnull, "w")):
nltk.download('wordnet')
Or some other options:
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