我试图弄清楚为什么列出从硬盘驱动器到网站的目录时输出会提前结束。我删除了调试模式的错误消息,并得到以下错误:
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\Users\\Huang.LabTech2\\Desktop\\Images\\2017 Data\\Air Tractor 402/Air Tractor 402/5-10-2017/IR Filter'
我不确定为什么在运行时会查找第二个Air Tractor 402文件夹,但是其他文件夹也会发生这种情况:
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\Users\\Huang.LabTech2\\Desktop\\Images\\2017 Data\\UAV/UAV/Bobby_Hardin/8-16'
我的代码:
from flask import Flask, abort, send_file, render_template, 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(r'C:/Users/Huang.LabTech2/Desktop/Images/2017 Data/')
# Joining the base and the requested path
abs_path = os.path.join("c:", BASE_DIR, req_path)
# Return 404 if path doesn't exist
# if not os.path.exists(abs_path):
# return render_template('404.html'), 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, file) for file 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)
答案 0 :(得分:2)
您获得“重复”是因为您两次添加了req_path
:一次在这里:
# Joining the base and the requested path
abs_path = os.path.join("c:", BASE_DIR, req_path)
还有一个在这里:
files = [os.path.join(req_path, file) for file in files]
第二个应该看起来像这样:
files = [f for f in files] # (or just be deleted)