Python 无法识别同一目录中的文本文件?

Python is not recognizing text file that is in the same directory?

我当前的项目树:

redditbot/  

-- commands/  
----__init__.py  
----comment_cache.txt  
----readcomments.py  

--mainbot.py

我试图做的是通过 readcomments.py 文件中的 open('comment_cache.txt') 读取 comment_cache.txt 文件。但由于某种原因,我收到了 FileNotFoundError。即使我尝试 print(os.path.isfile('comment_cache.txt')),它也只是 returns 错误。

我是不是犯了初学者的错误?也许是我一直想念的东西?

编辑:我感谢所有 answers/comments,但我认为这是我的 Python 解释器本身的问题。我一直在 redditbot/ 目录和 commands/ 包之间移动文件,直到它开始工作。同样出于某种原因,每当我调用 print() 时,PyCharm 都会告诉我它未定义...

我假设 mainbot.py 是您 运行 您的应用程序的入口点,所以

问题一

根据您的项目树,该文件应位于路径

open('commands/comment_cache.txt')

问题二

I kept moving around the file between the redditbot/ directory and the commands/ package until it just started working.

您可能最终将文件放置在与 mainbot.py

相同的文件夹中

问题三

Also for some reason whenever I call print(), PyCharm tells me that it is undefined...

这背后可能有很多可能性,请检查 python 解释器是否配置正确,python 库是否在项目的 PYTHONPATH 上可用