有偿求助,代码如下,主页展示和下载功能正常。但是只要把首页路由改了,比如改成“/1”,就只有首页可以用,点文件夹进入下层文件夹就报错。
【flask代码】
from flask import Flask
from flask import request
from flask import render_template, send_from_directory
import os
import time
app = Flask(__name__)
# 这里是预先将值存储在系统环境变量中了
app.secret_key = 'QWEaRdskjgkjgads3TYUdI2fdgfsgdafdsafdOPdkjgkjgdskjgkjgaf1234ffdfdasa56fdakjgkjgkfaaa'
DEFAULT_PATH = 'D:/'
current_path = ''
# 获取文件信息的函数
def get_files_data(path):
"""
获取指定路径下的所有文件、文件夹的信息
"""
global current_path
files = []
for the_name in os.listdir(path):
# 拼接路径
file_path = path+"/"+the_name
# 判断是文件夹还是文件
if os.path.isfile(file_path):
the_type = 'file'
else:
the_type = 'dir'
name = the_name
size = os.path.getsize(file_path)
size = file_size_fomat(size, the_type)
# 创建时间
ctime = time.localtime(os.path.getctime(file_path))
# 封装成字典形式追加给 files 列表
files.append({
"name": name,
"size": size,
# 拼接年月日信息
"ctime": "{}/{}/{}".format(ctime.tm_year, ctime.tm_mon, ctime.tm_mday),
"type": the_type
})
# 更新当前路径
curre