Django IOError - 没有这样的文件或目录

时间:2016-09-08 12:53:16

标签: django

我已经阅读了有关此主题的先前主题,并尝试修改代码,但没有再次成功。 问题是我在以下视图中出现IOError message at %s. No such file or directory错误:

def some_view(request): 
   MYDIR = os.path.dirname(__file__)
    with open(os.path.join(MYDIR, '/static/egais_files/client.xml'), 'w') as f:
    # ....

client.xml位于以下文件夹中:

\\10.8.0.1\share\djprj\djprj\static\static\egais_files\client.xml  

任何想法我做错了什么?

更新1: 包含some_view的.py文件位于\\10.8.0.1\share\djprj\djprj\djprj\egais\views.py

更新2 。 settings.py文件

import os   
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG = True

ROOT_URLCONF = 'supermarket_project.urls'
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(os.path.dirname(BASE_DIR),"static","templates")],
        #'DIRS': [os.path.join(BASE_DIR,"templates")],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',                 
            ],},},]

WSGI_APPLICATION = 'supermarket_project.wsgi.application'
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
STATIC_URL = '/static/'
if DEBUG:
    MEDIA_URL = '/media/'
    STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),"static","static-only")
    MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR),"static","media")
    STATICFILES_DIRS = (os.path.join(os.path.dirname(BASE_DIR),"static","static"),
     )

2 个答案:

答案 0 :(得分:1)

您的源文件位于\\10.8.0.1\share\djprj\djprj\djprj\egais\,即行:

MYDIR = os.path.dirname(__file__)

会将该路径存储在MYDIR变量中,但您的文件位于不同的目录中。首先,尝试使用:

MYDIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))

将为您提供路径\\10.8.0.1\share\djprj\djprj\。现在,您应该再向行添加一个/static

with open(os.path.join(MYDIR, '/static/egais_files/client.xml'), 'w') as f:

所以它看起来像:

with open(os.path.join(MYDIR, '/static/static/egais_files/client.xml'), 'w') as f:

它应该提供适当的文件路径。

答案 1 :(得分:0)

除非包含some_view的文件位于\\10.8.0.1\share\djprj\djprj\static\,否则您没有提供正确的路径。我怀疑它在\\10.8.0.1\share\djprj\djprj\,意味着您需要添加' / static /'走的路。