1. 程式人生 > >Python錯誤TypeError: write() argument must be str, not bytes

Python錯誤TypeError: write() argument must be str, not bytes

and 原來 打開文件 進制 存儲方式 byte pen python3 str

小記一下,今天使用open打開文件的時候出現了下面的錯誤。

註:我用的是Python3.6.1版本。

TypeError: write() argument must be str, not bytes

網上搜索才發現原來是文件打開的方式有問題。

之前文件打開的語句是:

filehandle = open(WAV_FILE, ‘w‘)

然後使用二進制方式打開就沒有這個問題:

filehandle = open(WAV_FILE, ‘wb+‘)

產生問題的原因是因為存儲方式默認是二進制方式。

Python錯誤TypeError: write() argument must be str, not bytes