首页 > 解决方案 > Pythonanywhere 将图像文件保存在一步父目录而不是正确的目录中。*开发服务器进展顺利)

问题描述

我已经制作了自定义字段来保存图像和缩略图。缩略图应保存在“/.../.../targetimage/thumbnails/”文件夹中,全尺寸图像应保存在“.../.../targetimage/”文件夹中。

当我在本地开发服务器上运行 customfield 文件时,图像进入正确的目录。但在 pythonanywhere 中,原始图像和缩略图图像进入同一目录。我不明白为什么 Pythonanywhere 的行为方式不同。

字段.py

def _add_path_to_thumb(s):
    print('this is path',s)
    fname_list=[]
    parts = s.split(".")
    print('this is parts',parts)
    pathparts=parts[0].split("\\")
    print('this is pathparts', pathparts)
    fname_list.append(pathparts[-1])
    fname_list.append('-thumb')
    fname_list.append('.jpg')
    fname ="".join(fname_list)
    del pathparts[-1]
    pathparts.extend(['thumbnails\\'])
    print('this is pathparts final', pathparts)
    path_prop = "\\".join(pathparts)
    print('this is pathparts final prop', path_prop)
    MEDIA_ROOT_THUMB = os.path.join(MEDIA_ROOT, 'target_image/thumbnails/')
    print('this is media_root_thumb', MEDIA_ROOT_THUMB)
    fullopathusingos = os.path.join(MEDIA_ROOT_THUMB,fname)
    print('this is full path using os ',fullopathusingos )

    fullpath = path_prop+fname
    return fullopathusingos


标签: imagefilepathpythonanywhere

解决方案


You're switching between using \ and / as path separators. It will behave one way on Windows and another way on PythonAnywhere. I suspect the main cause of the problem is splitting on \ because, on PythonAnywhere, you will not get a path where splitting on \ breaks it into directory parts.


推荐阅读