Python文件写入error

Python 2020-01-13 127 次浏览 次点赞

问题描述:

测试requests库,抓了一张百度的logo,在文件写入的时候报了权限不够的错
import requests
response = requests.get('https://www.baidu.com/img/bd_logo1.png')
print(response.content)
with open('C:/baidu.png', 'wb') as f:
    f.write(response.content)
    f.close()
Traceback (most recent call last):
  File "D:/PycharmProjects/untitled/test.py", line 42, in <module>
    with open('C:/baidu.png', 'wb') as f:
PermissionError: [Errno 13] Permission denied: 'C:/baidu.png'
解决方法:
  • 在当前项目下保存
with open('baidu.png', 'wb') as f:
    f.write(response.content)
    f.close()
  • 在非系统盘下保存
with open('D:/baidu.png', 'wb') as f:
    f.write(response.content)
    f.close()
  • ubuntu系统中给文件夹开放权限

本文由 fmujie 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

还不快抢沙发

添加新评论

召唤看板娘