catch UnicodeDecodeError

Andrew Berg bahamutzero8825 at
Wed Jul 25 13:34:53 CEST 2012

On 7/25/2012 6:05 AM, jaroslav.dobrek at wrote:
> What I really want to do is use something like
> try:
>     # open file, read line, or do something else, I don't care
> except UnicodeDecodeError:
>     sys.exit("Found a bad char in file " + file + " line " + str(line_number)
> Yet, no matter where I put this try-except, it doesn't work.
> How should I use try-except with UnicodeDecodeError?
The same way you handle any other exception. The traceback will tell you
the exact line that raised the exception. It helps us help you if you
include the full traceback and give more detail than "it doesn't work".

CPython 3.3.0b1 | Windows NT 6.1.7601.17803

More information about the Python-list mailing list