python - Python读取路径错误
问题描述
所以我有这段代码可以打开一个 JSON 文件。(已导入 JSON)
with open("Python\videoid.json", "r") as json_file:
问题是 python 正在读取\v
错误的部分并告诉我该文件不存在。有谁知道如何解决这个问题?
解决方案
在 python '\' 是转义键中,python 是否认为您正在尝试使用转义字符,您可以通过制作原始字符串来解决此问题:
with open(r”Python\videoid.json", "r") as json_file:
# the r tells python to make it a raw string
或者您可以添加另一个反斜杠:
with open("Python\\videoid.json", "r") as json_file:
# tells python you want to actually do a backslash key
行上的注释解释了它。
推荐阅读
- postgresql - 无法从列表中的字典中选择数据
- python - 在 python 存储库名称和包名称中使用连字符/破折号
- sql - 如何设置带有前缀和递增十进制值的 ID?
- vuejs2 - 承诺中 VueJs 2 的“this”的价值
- python - 使用 python django 解析 RSS XML
- javascript - 为什么 VueJS 在推送数组后自动重新加载?
- node.js - 如何安全地使用 Apache Cassandra?
- php - 数据变量返回为 [object Object]
- swift - 在 Xcode/swift 中显示一个视图控制器,使用 if else 进行登录
- reactjs - 导入自定义工具栏组件不会触发 react-big-calendar 中的方法