I’m attempting to make a python package of the following hierarchy:
\standard
\__init__.py
\text.txt
\scan.py
There is a function within scan.py called parse() which opens text.txt via:
name_list = open('text.txt','r')
However when I run
from standard import *
result = scan.parse()
I get the following:
IOError: [Errno 2] No such file or directory: '/text.txt'
Python has the funny variable
__file__which is the name of the file containing the running code. Your code is looking in the current working directory instead.Use this to open your file:
Docs related to special variable
__file__:http://docs.python.org/reference/datamodel.html