如何在Django应用程序中使用相对路径来读取本地文件?

时间:2022-11-28 22:50:24

My django app has to read some text files from the file system. So I make a directory in my app directery, and use relative path to open and read from the file.

我的django应用程序必须从文件系统中读取一些文本文件。所以我在我的app directery中创建了一个目录,并使用相对路径打开并从文件中读取。

areas = parseXmlFile('xml_files/area.xml')

When I run server to debug using manage.py runserver, that's ok. But I run server using manage.py runfcgi host=127.0.0.1 port=8081 , Django can't find the file: No such file or directory: 'xml_files/area.xml'

当我使用manage.py runserver运行服务器进行调试时,没关系。但是我使用manage.py运行服务器runfcgi host = 127.0.0.1 port = 8081,Django找不到文件:没有这样的文件或目录:'xml_files / area.xml'

I don't want to use absolute path, that means I need to modify much code.

我不想使用绝对路径,这意味着我需要修改很多代码。

How can I solve the problem to use relative path to open local file?

如何使用相对路径打开本地文件来解决问题?

2 个解决方案

#1


15  

I'll suggest you to use absolute path but in a more clever way. Declare in your settings.py something like XMLFILES_FOLDER and have your settings.py like this:

我建议你以更聪明的方式使用绝对路径。在您的settings.py中声明XMLFILES_FOLDER之类的内容,并将您的settings.py设置为:

import os
settings_dir = os.path.dirname(__file__)
PROJECT_ROOT = os.path.abspath(os.path.dirname(settings_dir))
XMLFILES_FOLDER = os.path.join(PROJECT_ROOT, 'xml_files/')

This is assuming that xml_files folder lives under the project root folder, if not, just declare the relative path from the project root folder to the xml_files

这假设xml_files文件夹位于项目根文件夹下,如果没有,只需声明从项目根文件夹到xml_files的相对路径

XMLFILES_FOLDER = os.path.join(PROJECT_ROOT, 'f1/f2/xml_files/')

That way, wherever in your code you want to access a file inside that directory you just do:

这样,无论您在代码中想要访问该目录中的文件,您只需执行以下操作:

from settings import XMLFILES_FOLDER
path = XMLFILES_FOLDER+'area.xml'

This approach will work in any OS, and no matter you change the folder of the project, it will still work.

这种方法适用于任何操作系统,无论您更改项目的文件夹,它仍然可以工作。

Hope this helps!

希望这可以帮助!

#2


8  

@Paulo Bu's answer is correct, but outdated. Modern-day settings.py files have a BASE_DIR variable you can use for this endeavour.

@Paulo Bu的回答是正确的,但过时了。现代的settings.py文件有一个BASE_DIR变量,您可以使用它来完成这项工作。

import os
from yourproject.settings import BASE_DIR
file_path = os.path.join(BASE_DIR, 'relative_path')

Bear in mind that the relative path is from your Django project's root folder.

请记住,相对路径来自Django项目的根文件夹。

#1


15  

I'll suggest you to use absolute path but in a more clever way. Declare in your settings.py something like XMLFILES_FOLDER and have your settings.py like this:

我建议你以更聪明的方式使用绝对路径。在您的settings.py中声明XMLFILES_FOLDER之类的内容,并将您的settings.py设置为:

import os
settings_dir = os.path.dirname(__file__)
PROJECT_ROOT = os.path.abspath(os.path.dirname(settings_dir))
XMLFILES_FOLDER = os.path.join(PROJECT_ROOT, 'xml_files/')

This is assuming that xml_files folder lives under the project root folder, if not, just declare the relative path from the project root folder to the xml_files

这假设xml_files文件夹位于项目根文件夹下,如果没有,只需声明从项目根文件夹到xml_files的相对路径

XMLFILES_FOLDER = os.path.join(PROJECT_ROOT, 'f1/f2/xml_files/')

That way, wherever in your code you want to access a file inside that directory you just do:

这样,无论您在代码中想要访问该目录中的文件,您只需执行以下操作:

from settings import XMLFILES_FOLDER
path = XMLFILES_FOLDER+'area.xml'

This approach will work in any OS, and no matter you change the folder of the project, it will still work.

这种方法适用于任何操作系统,无论您更改项目的文件夹,它仍然可以工作。

Hope this helps!

希望这可以帮助!

#2


8  

@Paulo Bu's answer is correct, but outdated. Modern-day settings.py files have a BASE_DIR variable you can use for this endeavour.

@Paulo Bu的回答是正确的,但过时了。现代的settings.py文件有一个BASE_DIR变量,您可以使用它来完成这项工作。

import os
from yourproject.settings import BASE_DIR
file_path = os.path.join(BASE_DIR, 'relative_path')

Bear in mind that the relative path is from your Django project's root folder.

请记住,相对路径来自Django项目的根文件夹。