静态目录文件浏览过早结束



我目前正在制作一个小网站来托管大量无人机图像马赛克,并希望进来的人能够通过我得到的数据文件夹结构来查找他们的数据。但是,我在访问任何文件之前中止。以下代码在过去的"2017 数据"中是 404 个文件夹时给出 3 错误。在某些情况下,这是存储图像的上方 1 或 2 个文件夹。

from flask import Flask, abort, send_file, render_template_string
import os
import remoteSensingData
app = Flask(__name__)
@app.route('/', defaults={'req_path': ''})
@app.route('/<path:req_path>')
def dir_listing(req_path):
BASE_DIR = os.path.abspath("/Users/Huang.LabTech2/Desktop/Images/2017 Data/")
# Joining the base and the requested path
abs_path = os.path.join(BASE_DIR, req_path)
# 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)
# Show directory contents
files = os.listdir(abs_path)
files = [os.path.join(req_path, f) for f in files]
# return render_template('files.html', files=files)
return render_template_string("""
<ul>
{% for file in files %}
<li><a href="{{ file }}">{{ file }}</a></li>
{% endfor %}
</ul>
""", files=files)

if __name__ == '__main__':
app.run(debug=True)

我在实现最终目标方面还有其他问题,但它们与这个问题无关,所以我将为他们形成单独的问题。

我认为您的问题是由BASE_DIR路径中的空格引起的。

您可以通过将文件夹从2017 Data重命名为2017_Data来轻松证明这一点 或者您可以尝试使用:

BASE_DIR = os.path.abspath(r"/Users/Huang.LabTech2/Desktop/Images/2017 Data/")

最新更新