python-3.x - Windows FileNotFoundError: [Errno 2] 没有这样的文件或目录
问题描述
我想用'os.path.join'
打开一个文件,文件存在,但是我打不开,报错"FileNotFoundError"
。
这是运行python3.6,在Windows10中使用PyCharm。
此函数中发生错误:
def get_encoder(model_name):
with open(os.path.join('models', model_name, 'encoder.json'), 'r') as f:
encoder = json.load(f)
输出是' FileNotFoundError: [Errno 2] No such file or directory: 'models\ \345M\ \encoder.json'
我的文件目录是' ...\models\345M\encoder.json '
函数定义为' ...\encode.py '
解决方案
看来问题来自不包括正确的根文件夹。由于encoder.py
文件在src
文件夹内,并且路径models
在src
.
代码应该是:
def get_encoder(model_name):
with open(os.path.join('..\\models', model_name, 'encoder.json'), 'r') as f:
encoder = json.load(f)
让我知道这是否适合您。
推荐阅读
- python - 如何将 CSV 数据集随机拆分为 70-30?
- javascript - JavaScript 未从 HTML 更新值
- c++ - 如何使用 c++ (curl) 发送电子邮件
- java - 如何在 Java 中使用对象属性填充 JTable?
- pwa - PWA - 保护用户的部分代码
- sql - 访问 VBA '连接操作中的语法错误'
- bash - awk 长度正在计数 +1
- ibm-cloud - 如何将 Botkit 中间件与 Watson Assistant 对话服务器操作结合使用?
- python - 使用 pytest 测试 __init__.py 中可选依赖项的导入:Python 3.5 /3.6 的行为不同
- android - 作业调度程序无法取消系统 uid 的作业