python - 在 Python 中打开文件错误:没有这样的文件或目录
问题描述
我是在这里写作的新手。所以请多多包涵。
我正在运行此代码以打开我的文件,并将其放在正确的目录“数据”中。但是我的 python 不断地向我发送错误消息。
我写了这个,
#file = unidecode.unidecode(open('./data/input.txt').read())
#file = unidecode.unidecode(open('./data/linux.txt').read())
file = unidecode.unidecode(open('./data/hh1.txt').read())
file_len = len(file)
print('file_len =', file_len)
并弹出这个
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-36-aa7f0f650918> in <module>()
4 #file = unidecode.unidecode(open('./data/linux.txt').read())
5
----> 6 file = unidecode.unidecode(open('./data/hh1.txt').read())
7 file_len = len(file)
8 print('file_len =', file_len)
FileNotFoundError: [Errno 2] No such file or directory: './data/hh1.txt
它是 RNN(循环神经网络)代码的一部分,也是处理文本数据以学习针织图案的一部分。
这是一个非常简单的错误,但我找不到好的出路....所以感谢您的耐心阅读本文,我希望有人可以帮助我
解决方案
您正在尝试打开相对路径中数据文件夹内的文件。
open('./data/hh1.txt').read()
如果您的脚本在
/home/user/test.py
这个尝试打开:
/home/user/data/hh1.txt
如果你使用
open('./hh1.txt').read()
这个试图打开
/home/user/hh1.txt
那是在您的脚本的同一目录中。
您可以使用:
import os
print(os.listdir())
它会显示当前目录中的所有文件。
如果您使用的是相对路径,请检查从当前目录到目标文件的路径。
推荐阅读
- flutter - 参数类型 'num?' 不能分配给参数类型 'num'.dart(argument_type_not_assignable)
- svelte - 转到网址:单击 Sapper
- node.js - 在 mongodb 数组中查找嵌套字段
- ios - Firebase SwiftUI - 使用子集合创建文档
- javascript - 如何使用 javascript 添加 class="lazyload" 和 data-sizes="auto"?
- kubernetes - Kubernetes 裸机:为节点分配外部 IP
- c - int* 动态重新分配在 C 中的大小为 8 时失败?
- java - SSLHandshakeException: sun.security.validator.ValidatorException : 对于某些 URL 的集成测试,但在 Spring Boot 中运行
- nginx - 我的 http://www.example.com 重定向到 https,但我的 http://example.com 没有。为什么
- python - 如何验证我的 beginSim() 是否正确生成了我的排行榜列表?