I am trying to list all the files and sub-directories in particular directory("/data/file/transfer").
Now I have this problem only getting this file which is in the second level sub-directory. I am using Jinja template to view the page.
Directory structure looks like :
/data/
----file/
--------transfer/
-------------first/
----------------second/
-------------------get_this_file.txt
the file get_this_file.txt is in /data/file/transfer/first/second/ directory.
#! /usr/bin/env python
from flask import Flask,render_template, send_file
import os
import sys
app = Flask(__name__)
@app.route("/list_data", defaults={'req_path': ''})
@app.route('/<path:req_path>')
def incident_data(req_path):
base_dir = "/data/file/transfer/"
abs_path = os.path.join(base_dir, req_path)
if os.path.isfile(abs_path):
return send_file(abs_path)
final_files = []
if os.path.isdir(abs_path):
files = os.listdir(abs_path)
for file in files:
temp_file = req_path + "/" + file
print temp_file
final_files.append(temp_file)
return render_template('files.html', files=final_files)
if __name__ == "__main__":
app.config.update(dict(
DEBUG=True,
SECRET_KEY=b'_isecret/'
))
app.run(host="0.0.0.0", port=8080)
my templates/files.html is below:
<ul>
{% for file in files %}
<li><a href="{{ file }}">{{ file }}</a></li>
{% endfor %}
</ul>
OUTPUT:
on chrome:
192.168.168.xxx:8080/first/second
first/second/get_this_file.txt <--- (when clicked)
192.168.168.xxx:8080/first/first/second/get_this_file.txt
Cant understand why href for get_this_file.txt is wrong.
Any help is appreciated. Struggling from a long time on this.