galaxyan
galaxyan

Reputation: 6121

How to stop program if there is a warning

I try to use nltk to do some words processing, but there is a warning. I find out the if there is the word like "Nations�", the program would throw a warning. I wonder if there is any way to stop the program after the warning caused. Thank you

warning:

*UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
  if word[0].lower() not in stopwords.words():*

Upvotes: 14

Views: 10441

Answers (2)

yairchu
yairchu

Reputation: 24774

python -Werror ...

(does appear in accepted answer but is slightly hidden there)

Upvotes: 4

Davidmh
Davidmh

Reputation: 3865

A warning is a non-fatal error. Something is wrong, but the program can continue.

They can be handled with the standard library module warnings or through the command line, passing the flag -Werror. Programatically:

import warnings

with warnings.catch_warnings():
    warnings.simplefilter('error')
    function_raising_warning()

Upvotes: 16

Related Questions