Loading local data file with numpy

I’m trying to load a data file using numpy. The data file is stored locally. I have the following code which allows me to load a local CSS file:

file_directory = os.path.dirname(os.path.abspath(__file__)) + "/staticFiles"
static_file_route = '/static/'

@app.server.route('{}<local_file>'.format(static_file_route))
def serve_file(local_file):
    return flask.send_from_directory(file_directory, local_file)

app.css.append_css({"external_url": "/static/css_file.css"})

When I try to load a data file from the same directory:

data = np.genfromtxt("/static/data.dat",skip_header=1)

I get “OSError: /static/data.dat not found.”

It’s telling you that it can’t find a file at the path you specified. You probably want a relative path like static/data.dat (without the leading slash). With the slash, it’s going to be looking from the root of your filesystem, which is presumably not where the file is.

I tried making that change, but still get the same error.

I haven’t been able to find any example with local data files being loaded into numpy. Does anyone know of any?

If you’re using a relative path, then you have to run the Python process from the same directory that your paths are relative to. So either change to that directory first in your terminal before running or use an absolute path, which is going to make your app more robust anyway.

If you still get that error with an absolute path, something weird is going on. I would then try and do a regular Python open() of the file as sanity check to make sure you can even read anything in.

1 Like

That was the problem. I ran my program from the proper directory and it works fine now. Thank you very much for your assistance!

1 Like