-1

My current project tree:

redditbot/  

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

--mainbot.py

What I am attempting to do is read the comment_cache.txt file via open('comment_cache.txt')in the readcomments.py file., but for some reason I am getting a FileNotFoundError. Even when I try print(os.path.isfile('comment_cache.txt')), it just returns false.

Am I making a beginner mistake here? Maybe something that just I keep missing?

EDIT: I appreciate all the answers/comments, but I believe it is a problem with my Python interpreter itself. I kept moving around the file between the redditbot/ directory and the commands/ package until it just started working. Also for some reason whenever I call print(), PyCharm tells me that it is undefined...

Aaron
  • 445
  • 1
  • 4
  • 11
  • 2
    And you run this how? `open()` with a a relative path is relative to the current directory of the process of the Python interpreter (i.e. `os.getwcd()`), not relative to the directory the current module file is in. – dhke Aug 24 '16 at 00:17
  • 1
    Per your tree, it should be `commands/comment_cache.txt` – double_j Aug 24 '16 at 00:26

1 Answers1

0

I am assuming that mainbot.py is the entry point from where you run your application, so

Problem I

based on your project tree, the file should be available at path

open('commands/comment_cache.txt')

Problem II

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

You probably ended up placing the file in the same folder as your mainbot.py

Problem III

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

There can be many posibilities behind this, check if the python interpreter is configured correctly and the python libraries are available on the PYTHONPATH for the project

Saif Asif
  • 5,516
  • 3
  • 31
  • 48