错误描述:

FileNotFoundError: [Errno 2] No such file or directory: ‘XXX’ 的解决方法

在编写爬虫文件的过程中,一般会将爬取下来的文件保存在一个文件夹内,而当选取的文件夹不存在时,会报错"FileNotFoundError: [Errno 2] No such file or directory".

例如

def __init__(self, path = './'):#save the pathself.path = pathdef save2Img(self, fname, url):#save the imageimg = self.reqPage(url)#file's name and suffixfname = fname + '.' +url.rsplit('.',1)[-1]#file's pathfpath = os.path.join(self.path,fname)#save the imagewith open(fpath,'wb') as f:     #<-----报错位置f.write(img)

指定的路径是当前路径,而保存路径为‘img’文件夹。如果之前’img‘文件夹不存在,就会报文件未找到的错误提示。

解决方法

解决该问题的方法主要分为两种:

第一种:代码解决

在代码中加入预检测,如果文件夹路径不存在,则创建文件夹路径。

def __init__(self, path = './'):#save the pathself.path = pathif not os.path.exists(path):    os.mkdir(path)

第二种:手动添加

在编码的过程中,确定要保存文件的文件夹路径,提前创建文件夹;或者直接保存在已有的文件夹内。

FileNotFoundError: [Errno 2] No such file or directory: 'XXX' 的解决方法相关推荐

  1. FileNotFoundError: [Errno 2] No such file or directory: _MEI138162\\astor\\VERSION‘

    FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\me\\AppData\\Local\\Temp\\_MEI13 ...

  2. python导入pods的时候出现错误:FileNotFoundError: [Errno 2] No such file or directory: ‘D:\\tmp\\sods.log‘

    导入Pods库的时候出现错误: import pods FileNotFoundError: [Errno 2] No such file or directory: 'D:\tmp\sods.log ...

  3. python3 selenium 无头浏览器 错误 FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver'

    代码: from selenium import webdriver browser = webdriver.Firefox() browser.get('http://localhost:8000' ...

  4. 成功解决FileNotFoundError: [Errno 2] No such file or directory: '/home/bai/Myprojects/Tfexamples/data/kn

    成功解决FileNotFoundError: [Errno 2] No such file or directory: '/home/bai/Myprojects/Tfexamples/data/kn ...

  5. 成功解决FileNotFoundError: [Errno 2] No such file or directory: 'F:\\Program Files\\Python\\Python36\\li

    成功解决FileNotFoundError: [Errno 2] No such file or directory: 'F:\\Program Files\\Python\\Python36\\li ...

  6. 成功解FileNotFoundError: [Errno 2] No such file or directory: './data\\mnist\\train-images-idx3-ubyte'

    成功解FileNotFoundError: [Errno 2] No such file or directory:  './data\\mnist\\train-images-idx3-ubyte' ...

  7. Python:错误FileNotFoundError: [Errno 2] No such file or directory: 'objects/epsilon.pkl

    目录: 前言 解释 详细解释 前言 看微信推送了一个好玩的应用强化学习dinosrun,但是在运行时,遇到了这个问题,百思不得其解: FileNotFoundError: [Errno 2] No s ...

  8. FileNotFoundError: [Errno 2] No such file or directory: 'traingingDigits/0_0.txt'

    使用KNN进行手写体识别的时候,出现FileNotFoundError: [Errno 2] No such file or directory: 'traingingDigits/0_0.txt', ...

  9. 使用pyLDAvis可视化LDA结果,与解决FileNotFoundError: [Errno 2] No such file or directory: ‘https://cdn.jsdel....

    建议安装: pip install pyLDAvis==2.1.2 否则会报错:FileNotFoundError: [Errno 2] No such file or directory: 'htt ...

最新文章

  1. MySQL数据库基本操作总结(不断更新中......)
  2. AIX 操作系统日常维护须知
  3. android toast通知关闭,屏蔽系统通知,Toast无法显示的解决方案 v2.0.0
  4. 多家防火墙设备存在信息泄露漏洞
  5. SpringMVC参数的传递——接收List数组类型的数据
  6. iPhone 11专用“浴霸”镜头保护膜曝光:史无前例
  7. androidStudio快捷键概览
  8. 十分钟弄懂最快的APP自动化工具uiautomator2
  9. js获取非行间样式--有bug,忧伤
  10. jQuery表格排序组件-tablesorter
  11. php xdebug调试 phpstorm配置
  12. matlab的课程,matlab简介(中科大MATLAB课程).ppt
  13. java正则表达式版本_java 正则表达式 版本号_java正则表达式?=.*_密码的正则表达式java...
  14. QT 简单实现自定义标题栏
  15. ORB-SLAM2学习笔记——BundleAdjustment函数
  16. notablilty笔记本模板_notability实测,让你上天的笔记软件
  17. xamarin学习笔记A19(安卓AIDL)
  18. 微信支付平台设置及如何获取微信支付所需参数
  19. linux云计算架构师:搭建DHCP服务和NTP网络时间同步
  20. Opencv 图片处理

热门文章

  1. 智能对话机器人实战开发(1)- 体系结构和分类
  2. 【Axure高保真原型】中继器版下拉列表
  3. 【转】float与double的范围和精度
  4. 用Photoshop CS5新功能完成精细毛发抠图
  5. 教你分清楚SPI、I2C、UART、I2S、GPIO、SDIO、CAN!
  6. PMP项目管理敏捷项目管理
  7. python 漏洞扫描器_SRCHunter Python开源漏洞扫描器
  8. extremecomponents 介绍
  9. php start 五
  10. 从外网访问使用Padavan固件的路由器(花生壳DDNS配置教程)