python - write argument must be str, not bytes

时间:2025-02-18 13:41:31

python -  write() argument must be str, not bytes

源代码

import requests
r = ('/img/bd_logo1.png')
if r.status_code == 200:
    with open('','w') as f:
        ()        
else:
    print('获取失败')

修改后代码

import requests
r = ('/img/bd_logo1.png')
if r.status_code == 200:
    with open('','wb') as f:
        ()        
else:
    print('获取失败')

# 原因分析 open的modle设置错误,需要将'w'改为'wb' ,'b'代表允许操作字节