12

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?

alvas
  • 115,346
  • 109
  • 446
  • 738
helpisgood
  • 361
  • 2
  • 15

2 Answers2

45

Use quiet=True:

import nltk
nltk.download('wordnet', quiet=True)
alvas
  • 115,346
  • 109
  • 446
  • 738
6

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:

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195