[英]FileNotFoundError: [WinError 3] The system cannot find the path specified: ''
[英]Python: FileNotFoundError: [WinError 3] The system cannot find the path specified:
我試圖弄清楚為什么列出從硬盤驅動器到網站的目錄時輸出會提前結束。 我刪除了調試模式的錯誤消息,並得到以下錯誤:
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)
之所以得到“重復”是因為您兩次添加了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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.