本文介绍了Django IOError - 没有这样的文件或目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了关于这个主题的以前的主题,并尝试修改代码,但是没有成功。
问题是我在%s获得 IOError消息。以下视图中没有此类文件或目录错误:

  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 

任何想法我做错了什么? p>

更新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 /'
如果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),


解决方案

您的源文件位于 \\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打开(os.path.join(MYDIR,'/static/egais_files/client.xml'),'w')为f:

,所以它看起来像:

  with open(os.path.join(MYDIR, / / pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ And $ And And And And And And And And And And And And And And And And And And And And And And And And And And And And And And它应该提供正确的文件路径。


I've read the previous threads on this topic and tried to modify the code, but no success again. The problem is that I get IOError message at %s. No such file or directory error in the following view:

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 is located in the following folder:

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

Any ideas what am I doing wrong ?

UPDATE 1:The .py file containing some_view is located in \\10.8.0.1\share\djprj\djprj\djprj\egais\views.py

UPDATE 2. The settings.py file

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"),
     )
解决方案

Your source file is located in \\10.8.0.1\share\djprj\djprj\djprj\egais\, that means line:

MYDIR = os.path.dirname(__file__)

will store that path in MYDIR variable, but your file is located in different directory. First, try to use:

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

that will give you path \\10.8.0.1\share\djprj\djprj\. Now, you should add one more /static to line:

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

so it will look like:

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

And it should give proper file path.

这篇关于Django IOError - 没有这样的文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 20:48