How to access a directory file outside of a django project?

I have a Django project running on RHEL 7 OS. The project is on the way /root/project. The project is hosted on the httpd server. Now I'm trying to access a file outside the directory, for example/root/data/info/test.txt

How do I access this path in views.py so that I can read and write a file that is outside the project directory? I tried to add the path in sys.path, but it did not work. Read and write permission is also granted to the file.

+4
source share
1 answer

Add the following lines to 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')
+3

Source: https://habr.com/ru/post/1652999/


All Articles