Anthony Bartoli Anthony Bartoli - 1 year ago 97
Python Question

Python program fails on Windows but not on Linux

The program below triggers a UnicodeEncodeError on my Windows 10 machine (running Python 3.5.2) but no error at all on my Linux machine (running Python 3.3.2).

import logging
str ="Antonín Dvořák"
logging.basicConfig(filename='log.txt', level=logging.INFO)

On Linux, the log file correctly contains:

INFO:root:Antonín Dvořák

On Windows, I get the following error:

enter image description here

Any ideas on what the possible cause could be for this discrepancy?

Answer Source

Instead of a file name, you could pass a stream whose encoding is specified:

    stream=open('log.txt', 'w', encoding='utf-8'),

As for the cause, it's probably trying to open the target file using your current locale's encoding (CP1252, judging by the stack trace).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download