Yunter
Yunter

Reputation: 284

django reading file from root: FileNotFoundError

Django 1.9:

I am trying to open and read a file in a views.py function. I am getting a FileNotFound error, however I think the path is correct. I have put the file in the root:

C:.
|   file1.txt
|   settings.py
|   urls.py
|   wsgi.py
|   __init__.py
|   

I have the following in my settings.py

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

In my views I am trying to open the file with the following line:

def post(self, request):
        file_ = open(os.path.join(settings.BASE_DIR, 'file1.txt'))

But this error occurs:

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\x\\y\\z\\Year 4\\Semester 2\\cvCleaner\\cvcleaner\\file1.txt'

Is it to do with having spaces in my directory names? I'm not sure.

Upvotes: 4

Views: 1422

Answers (1)

Yunter
Yunter

Reputation: 284

My embarrassment might save someone in the future:

The path was in fact wrong.

I had the file in the same path as settings.py, moving it to the same path as manage.py works fine.

Upvotes: 2

Related Questions