I am writing a django project that involves retrieving data from a table. I have a module that has the line to retrieve some data (snp_data.txt is a file on the same directory of the module):
data = file("snp_data.txt")
While the module works well when I call it separately outside the django project; I keep getting the error below, when I call with other module within the django app.
no such file or directory as 'snp_data.txt'
Any idea what's going on?
You are trying to open the file in the current working directory, because you didn't specify a path. You need to use an absolute path instead:
import os.path
BASE = os.path.dirname(os.path.abspath(__file__))
data = open(os.path.join(BASE, "snp_data.txt"))
because the current working directory is rarely the same as the module directory.
Note that I used open()
instead of file()
; the former is the recommended method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With