7

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
  • 94,813
  • 90
  • 365
  • 641
helpisgood
  • 301
  • 2
  • 14

2 Answers2

28

Use quiet=True:

import nltk
nltk.download('wordnet', quiet=True)
alvas
  • 94,813
  • 90
  • 365
  • 641
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
  • 414,977
  • 106
  • 935
  • 1,083