首页 > 解决方案 > 在 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(循环神经网络)代码的一部分,也是处理文本数据以学习针织图案的一部分。

这是一个非常简单的错误,但我找不到好的出路....所以感谢您的耐心阅读本文,我希望有人可以帮助我

标签: pythonrecurrent-neural-network

解决方案


您正在尝试打开相对路径中数据文件夹内的文件。

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())

它会显示当前目录中的所有文件。

如果您使用的是相对路径,请检查从当前目录到目标文件的路径。


推荐阅读