Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access directory file outside django project?

I have my Django project running on RHEL 7 OS. The project is in path /root/project. And project is hosted on httpd server. Now iam trying to access a file out side the directory like /root/data/info/test.txt

How should I access this path in views.py so that I can read and write file which is outside the project directory ? I tried to add the path in sys.path but it didn't work. Read and write permission are also give to the file.

like image 743
ketan khandagale Avatar asked Aug 30 '16 13:08

ketan khandagale


1 Answers

Add the following lines to your settings.py

import os
..
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
FILES_DIR = os.path.abspath(os.path.join(BASE_DIR, '../data/info'))

Then you can use in your view

from django.conf import settings
import os
..
file_path = os.path.join(settings.FILES_DIR, 'test.txt')
like image 66
atn Avatar answered Oct 15 '22 03:10

atn