Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

Django IOError - No such file or directory

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.1sharedjprjdjprjstaticstaticegais_filesclient.xml  

Any ideas what am I doing wrong ?

UPDATE 1: The .py file containing some_view is located in \10.8.0.1sharedjprjdjprjdjprjegaisviews.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"),
     )
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your source file is located in \10.8.0.1sharedjprjdjprjdjprjegais, 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.1sharedjprjdjprj. 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...