简体   繁体   English

Python 中打开文件错误:没有这样的文件或目录

[英]Opening file error in Python : No such file or directory

I'm a newbie for writing here.我是在这里写作的新手。 So please please be bear with me.所以请多多包涵。

I'm running this code to open my file and I put this in right directory 'data'.我正在运行此代码以打开我的文件,并将其放在正确的目录“数据”中。 But my python sent me error message continuely.但是我的 python 不断地向我发送错误消息。

I wrote this,我写了这个,

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

and poped up this并弹出这个

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

code image directory代码图像目录

It's part of RNN(Recurrent Neural Network) code and it's part of processing text data to learn knitting pattern.它是 RNN(循环神经网络)代码的一部分,也是处理文本数据以学习针织图案的一部分。

It's very simple error but I can't find a good way out.... So thank you for your patient to read this and I hope someone could help me out这是一个非常简单的错误,但我找不到好的出路....所以感谢您的耐心阅读本文,我希望有人可以帮助我

You're trying to open a file that is inside data folder in your relative path.您正在尝试打开相对路径中数据文件夹内的文件。

open('./data/hh1.txt').read()

If your script is in如果您的脚本在

/home/user/test.py

This one tries to open:这个尝试打开:

/home/user/data/hh1.txt

And if you use如果你使用

open('./hh1.txt').read()

This one tries to open这个试图打开

/home/user/hh1.txt

That is in the same directory of your script.那是在您的脚本的同一目录中。

You can use:您可以使用:

import os
print(os.listdir())

And it will show you all files in the current directory.它会显示当前目录中的所有文件。

If you're using a relative path, check the path from your current directory to the destination file.如果您使用的是相对路径,请检查从当前目录到目标文件的路径。

It's likely that the path that you have entered is being interpreted differently than expected.您输入的路径的解释可能与预期不同。 This depends on a variety of this such as where the Python file you are executing is and whether it is part of a bigger project.这取决于各种情况,例如您正在执行的 Python 文件在哪里以及它是否是更大项目的一部分。

A good way to debug this is to expand the path you are trying to use into its absolute path.调试此问题的一个好方法是将您尝试使用的路径扩展到其绝对路径。 You can do this using the following code:您可以使用以下代码执行此操作:

import os
print(os.path.abspath("./data/hh1.txt"))

This will output something like "/home/user/project/data/hh1.txt" .这将 output 类似于"/home/user/project/data/hh1.txt"

You can check the output of this and verify that your files are in the right location or if your path is possibly incorrect.您可以检查 output 并验证您的文件是否位于正确的位置,或者您的路径是否可能不正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM