Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django [Errno 2] No such file or directory:

Tags:

python

django

I've built a script to read an excel file and save the contents into my database. (Note: the file and the script are in different directories). However, when I try to execute the script from my views.py as a simple import, django throws an error that it cannot find the file or directory:

[Errno 2] No such file or directory: '\\media\\documents\\GDRAT.xls\\'

My actual code in the script looks like this:

source_wb = xlrd.open_workbook('media/documents/GDRAT.xls')

Where my script is in the parent directory. Executing the script from the command line works just fine, so I'm struggling with why django is reading it differently.

My views.py function looks like this (Note: I go back to the parent directory to find the script - which seems to work fine, just can't find the excel file I need to read in):

def UpdateGDRAT(request):
    os.chdir('..')
    import GDRAT
    return render_to_response('success.html')

Any guidance is greatly appreciated!

like image 595
user3238647 Avatar asked Jan 26 '14 22:01

user3238647


1 Answers

This works for me

 os.path.join(os.path.dirname(os.path.dirname(__file__)),'media/documents/GDRAT.xls')
like image 141
Naushad Avatar answered Sep 23 '22 01:09

Naushad