天天看點

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

問題:

今天使用Python向檔案中寫入内容報了如下錯誤:

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

解決:

原因是檔案打開方式有問題,把之前的打開語句修改為二進制方式打開就沒有問題了。如下:

修改前的語句:

f_w = open(file_name, "w")
           

修改後的語句:

f_w = open(file_name, "wb+")