Switching to Python 3 causing UnicodeDecodeError

前端 未结 3 1106
臣服心动
臣服心动 2020-11-27 05:04

I\'ve just added Python3 interpreter to Sublime, and the following code stopped working:

for directory in directoryList:
    fileList = os.listdir(directory)         


        
3条回答
  •  醉话见心
    2020-11-27 05:31

    In general, I found 3 ways to fix Unicode related Errors in Python3:

    1. Use the encoding explicitly like currentFile = open(filename, 'rt',encoding='utf-8')

    2. As the bytes have no encoding, convert the string data to bytes before writing to file like data = 'string'.encode('utf-8')

    3. Especially in Linux environment, check $LANG. Such issue usually arises when LANG=C which makes default encoding as 'ascii' instead of 'utf-8'. One can change it with other appropriate value like LANG='en_IN'

提交回复
热议问题