4种方法转义字符解决报错FileNotFoundError: [Errno 2] No such file or directory
写在前面:
前面的案例不想看可以直接看结尾的4个结论。
直接看第4个结论,就是文章的最后。二八定律。【方法4通用性极强】
快速跳转至 通用性方法4
QQ:1981791622。
备注:我是CSDN道友
源代码:
with open ("pi_digits.txt","r") as file_object:#注意是C:\\文件夹1\\文件夹2,不是C:\,一定要记住!!!contents = file_object.read()print(contents)#>>>
FileNotFoundError: [Errno 2] No such file or directory: 'pi_digits.txt'
由No such file or directory分析,文件是有的,那就是路径。
该怎么修改?如下:
因此完整路径是:
C:\Users\YJG\Desktop\pi_digits.txt
多说一句:要找到你的文件地址,只需要把你要打开的文件直接拖到编辑器终端的窗口里,就会显示出文件地址,然后复制一下就好。(这个操作秀)
于是修改代码:
with open ("C:\Users\YJG\Desktop\pi_digits.txt","r") as file_object:#注意是C:\\文件夹1\\文件夹2,不是C:\,一定要记住!!!contents = file_object.read()print(contents)
还是报错:报错原因是路径,且往下文看。
由C语言的学习经验,将路径修改如下:
C:\\
Users\\
YJG\\
Desktop\\
pi_digits.txt
对比
C:\Users\YJG\Desktop\pi_digits.txt
多了一个\。
再看结果,报错我打嘴:
with open ("C:\\Users\\YJG\\Desktop\\pi_digits.txt","r") as file_object:#注意是C:\\文件夹1\\文件夹2,不是C:\,一定要记住!!!contents = file_object.read()print(contents)
为什么加了一个斜杠\就解决报错了?答案先不说,请先思考3秒:
3。。。。2。。。。。1
答案超级简单:转义字符啊。
正确路径是:
C:\Users\YJG\Desktop\pi_digits.txt
用字符串表示,注意一个细节是:"\\"
------>\
此时,你是不是马上恍然大悟。赶紧关注我,为思考点赞!
所以,用字符串表示C:\Users\YJG\Desktop\pi_digits.txt
是
"C:\\Users\\YJG\\Desktop\\pi_digits.txt"
最后结果成功出现也验证了这个原理。
多说一点:敲黑板!!!总结!!!
因为单独一个"\
",在Python中带有转义字符的意思。如:’\t’就代表按下一个tab键,"\\"
就表示\
,"\b"表示退格。
在Windows操作系统中,路径分隔符有两种:一种用\\
,另一种是用/,你也可以只用一个/来分隔路径。
来一起看一下效果:
with open ("C:/Users/YJG/Desktop/pi_digits.txt","r") as file_object:#注意是C:\\文件夹1\\文件夹2,不是C:\,也可以用/。contents = file_object.read()print(contents)
还可以加个 r ,如下:
with open (r"C:\Users\YJG\Desktop\pi_digits.txt") as file_object: # 加个rcontents = file_object.read()print(contents.strip())
小结:
在Windows操作系统中,路径分隔符有两种:一种用\\
,另一种是用/。
但是呢,别忘了\在Python中是转义字符,所以时常会有冲突。为了避坑,Windows的绝对路径通常要稍作处理,写成以下三种格式;
格式1:常用,尤其是做项目的时候。推荐使用。
with open (r"C:\Users\YJG\Desktop\pi_digits.txt") as file_object:contents = file_object.read()print(contents.strip())
格式2:
with open ("C:\\Users\\YJG\\Desktop\\pi_digits.txt") as file_object:contents = file_object.read()print(contents.strip())格式3:
with open ("C:/Users/YJG/Desktop/pi_digits.txt") as file_object:contents = file_object.read()print(contents.strip())
#============================= 通用性强 =============================方法4:【懒,一劳永逸,通用性强】
import os
import sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, BASE_DIR)
### 下面是你 with open()函数的相关代码
with open("pi_digits.txt") as f:contents = f.read()
print(contents)
通用性方法4
补充第4种方法,先给代码,不讲原理:
import os
import sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, BASE_DIR)
### 下面是你 with open()函数的相关代码
with open("pi_digits.txt") as f:contents = f.read()
print(contents)
你只需这样做:【你只管复制粘贴,啥都不用改,4条命令通用的】
【复制粘贴以下4行代码到你报错文件的头四行即可】
import os
import sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, BASE_DIR)
有了上面的四行代码就可以解决这类的问题。通用性极强。【在python中遇到这类问题,无脑复制粘贴上面4行代码到报错文件的头四行即可解决问题】
复制粘贴后再运行你的py
文件 头四行,报错我打嘴。
另外,在Python中出现以下问题也可以通过上面的4条命令来解决:
问题:
ModuleNotFoundError: No module named xxx
解决:
import os
import sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, BASE_DIR)
# 在报错文件的头四行添加上述4条命令即可解决问题
复制粘贴后再运行你的py
文件 头四行,报错我打嘴。
下面的内容可看可不看。
也就是说,你在你报错的写 with open()
的py
文件中,头四行,必须是头四行添加以下代码,就可以 以文件名 的方式 读取文件里的内容:
import os
import sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, BASE_DIR)
### 下面是你 with open()函数的相关代码
with open("pi_digits.txt") as f:contents = f.read()
print(contents)
一句话,凡是遇到FileNotFoundError: [Errno 2] No such file or directory: 'pi_digits.txt'
这种问题,直接在该文件的头四行添加上面的4行代码即可完美解决问题。不用管你路径是什么。
第4种方法需要你懂python解释器是如何找到文件的路径的。
关于这一点,我有2点要说:【知道你懒得研究原理】
1.你其实无需关心 懂python解释器是如何找到文件的路径的
这句话是否理解,因为编程最重要的是先把功能实现跑起来,管他三七二十一
2.如果你想了解 懂python解释器是如何找到文件的路径的
这句话的答案,你可以看别人正确的理解的文章,比如我写的:
https://blog.csdn.net/YJG7D314/article/details/121431086
点击23-9一小节,截图+代码说的明明白白。
你是否掌握了呢?为思考点赞!
====================================== 我是有底线的 ======================================
4种方法转义字符解决报错FileNotFoundError: [Errno 2] No such file or directory相关推荐
- python读取csv文件路径正确但报错FileNotFoundError: [Errno 2] No such file or directory
项目场景: python读取csv文件路径正确但报错FileNotFoundError: [Errno 2] No such file or directory 问题描述: #储存调用表格.数据 #调 ...
- 解决python报错FileNotFoundError: [Errno 2] No such file or directory
原因: 仔细检查代码,原因是要创建的文件所在目录不存在导致的. 即:要创建/a/b/c/d.txt, 则需要先保证/a/b/c目录存在. 代码中先检测目录是否存在,不存在则先创建多级目录: if no ...
- libsodium引用报错FileNotFoundError: [Errno 2] No such file or directory: b‘liblibsodium.a‘
问题 操作系统环境:Ubuntu 22.04 在安装一个工具应用时,这个应用使用到了chacha20加密算法,所以需要依赖libsodium.按照网上教程下载源码并编译安装: sudo apt ins ...
- python解压函数extractall在windows上报错FileNotFoundError [Errno 2] No such file or directory
今天遇到一个很奇葩的问题,使用pyhon脚本下载服务器上的一个打包工具到本地,然后解压到本地使用,这个脚本在我个人电脑上可以正常运行,但是在别的电脑上就报错,报错信息如下: FileNotFoundE ...
- 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:‘image.jpg’
使用自己的主机远程连接服务器运行下面这段程序时, import os from PIL import Image file_dir = '/我的路径/...' for file in os.listd ...
- python 文件读取错误之FileNotFoundError: [Errno 2] No such file or directory:,顺便学习斜杠/和反斜杠\的用法
python 文件读取错误之FileNotFoundError: [Errno 2] No such file or directory:,顺便学习斜杠/和反斜杠\的用法: 最近学习文件读取和中文分词 ...
- 读取文件报错:FileNotFoundError: [Errno 2] No such file or directory
文章目录 问题描述 问题分析 解决办法 问题描述 使用 img = Image.open('data/DSC_8923.jpg') 读取一张图片时,报 FileNotFoundError: [Errn ...
- python打开文件时,找不到文件 FileNotFoundError: [Errno 2] No such file or directory报错
小白一枚,记录学习错误,共同进步 python打开文件时,找不到文件 FileNotFoundError: [Errno 2] No such file or directory报错 UnicodeD ...
最新文章
- 规格选项表管理之保存规格选项表数据
- 卷进大厂系列之LeetCode刷题笔记:移除元素(简单)
- 【性能优化】 之 10053 事件
- SpringSecurity案例之认证服务security配置
- 【Tomcat】Tomcat配置与优化(内存、并发、管理)【自己配置】
- 用javascript实现以下功能!_JavaScript实现汉字转拼音功能
- python加密文件_用 Python 加密文件
- Java 常用对象-StringBuffer类
- 构造函数和方法的区别
- 算法分析中的空间复杂度
- 如何使用工资短信生成器
- 学习Opencv笔记(二)————hsv色系
- gps高斯utm_高斯投影与UTM投影的区别
- 使用 Infiniband 实现 RDMA !IB卡介绍!下载IB 驱动 !lspci | grep Mell 查看 IB卡!
- 股票圣经后半部,继续讲讲共振和级别定于与盈利方式 	(2019-03-02 00:43:49)
- 行列式【线性代数系列(一)】
- 以太坊中metamask、imtoken等钱包签名的php验证
- Occupancy Map(Occupancy Grid)的更新
- Excel_VBA_编程教程(完整版)
- [转载]windows mobile中区分cmwap或cmnet接入点