FileNotFoundError: [Errno 2] No such file or directory: 'XXX' 的解决方法
错误描述:
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' 的解决方法相关推荐
- 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 ...
- 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 ...
- python3 selenium 无头浏览器 错误 FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver'
代码: from selenium import webdriver browser = webdriver.Firefox() browser.get('http://localhost:8000' ...
- 成功解决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 ...
- 成功解决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 ...
- 成功解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' ...
- Python:错误FileNotFoundError: [Errno 2] No such file or directory: 'objects/epsilon.pkl
目录: 前言 解释 详细解释 前言 看微信推送了一个好玩的应用强化学习dinosrun,但是在运行时,遇到了这个问题,百思不得其解: FileNotFoundError: [Errno 2] No s ...
- 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', ...
- 使用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 ...
最新文章
- MySQL数据库基本操作总结(不断更新中......)
- AIX 操作系统日常维护须知
- android toast通知关闭,屏蔽系统通知,Toast无法显示的解决方案 v2.0.0
- 多家防火墙设备存在信息泄露漏洞
- SpringMVC参数的传递——接收List数组类型的数据
- iPhone 11专用“浴霸”镜头保护膜曝光:史无前例
- androidStudio快捷键概览
- 十分钟弄懂最快的APP自动化工具uiautomator2
- js获取非行间样式--有bug,忧伤
- jQuery表格排序组件-tablesorter
- php xdebug调试 phpstorm配置
- matlab的课程,matlab简介(中科大MATLAB课程).ppt
- java正则表达式版本_java 正则表达式 版本号_java正则表达式?=.*_密码的正则表达式java...
- QT 简单实现自定义标题栏
- ORB-SLAM2学习笔记——BundleAdjustment函数
- notablilty笔记本模板_notability实测,让你上天的笔记软件
- xamarin学习笔记A19(安卓AIDL)
- 微信支付平台设置及如何获取微信支付所需参数
- linux云计算架构师:搭建DHCP服务和NTP网络时间同步
- Opencv 图片处理