django从root读取文件:FileNotFoundError

时间:2018-05-06 13:34:56

标签: python django path

Django 1.9:

我正在尝试打开并读取views.py函数中的文件。我收到FileNotFound错误,但我认为路径是正确的。 我把文件放在根目录中:

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

我在settings.py中有以下内容

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

在我的观点中,我尝试使用以下行打开文件:

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

但是发生了这个错误:

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

是否在目录名中包含空格?我不确定。

1 个答案:

答案 0 :(得分:2)

我的尴尬可能在将来拯救某人:

这条道路实际上是错误的。

我将文件放在与settings.py相同的路径中,将其移至与manage.py相同的路径。

相关问题