1

I'm working on a project, and have the following directory structure:

Classifier_Code/
    classifier/
        __init__.py
        Event.py
        classifier.py
        lib/
            __init__.py
            categories.txt
    test.py

The file __init__.py inside lib/ reads a list of several hundred category names from categories.txt and stores them in a list, which I then want to import into classifier.py using the statement

from lib import CATEGORY_LIST

However, when doing so, I get the following error:

Traceback (most recent call last):
  File "classifier/classifier.py", line 1, in <module>
    from lib import CATEGORIES
  File "classifier/lib/__init__.py", line 3, in <module>
    with open('categories.txt') as categories:
IOError: [Errno 2] No such file or directory: 'categories.txt'

That is, when I'm trying to import the module, I get an error because the text file categories.txt is going along with it.

Does anyone know a fix for this specific problem?

Ryan
  • 7,621
  • 5
  • 18
  • 31
  • `os.path.join(os.path.dirname(__file__), 'categories.txt')` – ekhumoro Jun 08 '15 at 02:02
  • I think a better title for your question would be something like "text file not found in imported module". You're not actually trying to "import" the text file and opening it in your python code (if you were, you could refer to this question: http://stackoverflow.com/q/1270951/1427124). Rather, you're importing a module, but expecting the module code to be able to find files located in its same directory. (Just pointing this out because at first glance I thought this might be a duplicate of the above linked question.) – DaoWen Jun 08 '15 at 02:06

2 Answers2

1

If you just use open('categories.txt') then it's going to look for the text file in the current working directory.

If you instead want it to look in the same directory where __init__.py is located, try changing your open to something like this:

with open(os.path.join(os.path.dirname(__file__), 'categories.txt')) as categories:

Assuming that code is in __init__.py, then __file__ should be the path to __init__.py, and you'll end up with the full path to your text file.

DaoWen
  • 32,589
  • 6
  • 74
  • 101
1

categories.txt will require a full path hence it cant locate where that is. Python always searches current directory. Try os.getcwd() to get current working directory and then Try os.listdir() to change working directory. This way your file will be recognized.

This link here gives detailed explanation of how Python handles files imports and how it works: Importing files from different folder in Python

Community
  • 1
  • 1
fscore
  • 2,567
  • 7
  • 40
  • 74