文件浏览器烧瓶

时间:2021-01-20 10:40:40

标签: python flask

我正在尝试创建一个已经运行良好的文件浏览器。 但是我遇到了一个问题。

我试图访问的第三个目录做了一些奇怪的事情。 我会让你知道问题是什么。

路径'.' (转到目录数据)

-->数据

-->app.py


路径“/data”(转到 DIR Data_Upload)

--> test.txt

--> 数据_上传


Path '/data/Data_Upload' (GO TO DIR test, 这就是问题发生的地方。现在它正在添加一些东西 不需要路径。)

--> 更多.txt

-- 测试


当我现在尝试访问目录“test”时 这样路径通常应该是'/data/Data_Upload/test'但是 路径是“/data/data/Data_Upload/test”,这当然会引发错误,因为 这个目录不存在。 问题是他在第一个之后添加了另一个 '/data',但为什么。 我不明白他为什么要那样做

这是我的代码。

@app.route('/', defaults={'req_path': ''})
@app.route('/<path:req_path>')
def dir_listing(req_path):
    BASE_DIR = os.path.relpath(".")
    print(BASE_DIR, '\nBASE-DIR 95')

    print(req_path, '\nreqpath')
    # Joining the base and the requested path
    abs_path = os.path.join(BASE_DIR, req_path)
    print(abs_path, '\nabs_path 99')
    # Return 404 if path doesn't exist
    if not os.path.exists(abs_path):
        return abort(404)

    # Check if path is a file and serve

    if os.path.isfile(abs_path):
        return send_file(abs_path)
    print(req_path, '\nreqpath')
    # Show directory contents
    files = os.listdir(abs_path)
    #print(files, '\nfiles 111')
    files = [os.path.join(req_path, f) for f in files]
    #print(files, '\nfiles 113')
    return render_template_string("""
    {% extends "base.html" %}
    {% block head %}
    {{ super() }}

    {% endblock %}
    {% block body %}
    {{ super() }}
    <ul>
        {% for file in files %}
        <li><a href="{{ file }}">{{ file }}</a></li>
        {% endfor %}
    </ul>
    {% endblock %}
    """, files=files)

0 个答案:

没有答案