Problem with paths in my script, relative to local python file I'm trying to import data stored as a JSON. It works fine in stand-alone python, but I am having problems in Blender python console.

I have a folder Applications/blender.app/Contents/Resources/2.74/scripts/addons/uhoh and in it I have wowtest.py, a blank __init__.py, and test_data.txt which contains the JSON.

This is wowtest.py

def wowtest(fname):

    import json

    print('hmmm.... will this work?')

    with open(fname, 'r') as infile:
        xdic = json.load(infile)

    return xdic


And this is what happens in Blender's python console:

&gt;&gt; import uhoh.wowtest as wow
&gt;&gt;&gt; wow.wowtest('test_data.txt')
hmmm.... will this work?

Traceback (most recent call last):
  File "&lt;blender_console&gt;", line 1, in &lt;module&gt;
  File "/Applications/blender.app/Contents/Resources/2.74/scripts/addons/uhoh/wowtest.py", line 9, in wowtest
    with open(fname, 'r') as infile:
FileNotFoundError: [Errno 2] No such file or directory: 'test_data.txt'


I have put a second copy of test_data' up one level (in/addons`) just in case, but this doesn't help. What am I missing?

NOTE: I will run this from a script next. If there are differences, please let me know.
The current file's location is obtained using __file__

import os
current_file_path = __file__
current_file_dir = os.path.dirname(__file__)


to access a file in the same directory as this python file you do

other_file_path = os.path.join(current_file_dir, "other_file.ext")
with open(other_file_path) as f:
   ...


if there's a directory on the same path as the main python file you can get that by doing

other_file_path = os.path.join(current_file_dir, "directory", "other_file.ext")
with open(other_file_path) as f:
   ...