python - 在文件数量增加时返回更新列表
问题描述
我想用列表中的新文件名返回更新后的列表,而不显式刷新服务器。这是我用烧瓶编写的后端代码:-
@app.route("/list")
def return_list():
all_files = os.listdir(r'H:/JS/uploads/')
files_without_extension = []
for file in all_files:
temp = os.path.splitext(file)
if(temp[1] == ".json" ):
files_without_extension.append(temp[0].title())
print("Total number of files : {}".format(len(all_files)))
print(files_without_extension)
return jsonify(files_without_extension)
问题是当我刷新我的网页时,列表正在更新。如果目录中删除或添加了文件,我希望更新列表。
解决方案
我认为您只能使用websockets来实现这一点。
另一种解决方案是定期更新页面。自己删除文件的用户将被重定向。
<meta http-equiv="refresh" content="5">
@app.route('/list')
def uploads_list():
def _froot(fname):
root,_ = os.path.splitext(fname)
return root
return jsonify(_froot(fname) for fname in glob.iglob('H:/JS/uploads/*.json'))
@app.route('/remove/<path:fname>', methods=['POST'])
def uploads_remove(fname):
if os.path.exists(f'{fname}.json'):
try:
os.remove(f'{fname}.json')
except OSError as err: pass
return redirect(url_for('uploads_list'))
推荐阅读
- r - 在 geom_histogram 上绘制 geom_point
- python - Python如何以整数形式获取当前时间
- angular - 如何使用 Angular 制作可点击的通知 Firebase
- excel - Excel VBA中仅特定单元格范围的下拉列表选择?
- java - OOP Java如何增加生命值和减少mp?
- java - 无论 Serializable 接口实现如何,我都会收到 java.io.NotSerializableException
- spring-cloud-dataflow - 如何在 Spring Cloud Data Flow UI 上显示日志
- android - 在 Kotlin 中使用 Handler 类会使我的 android 应用程序崩溃
- python - Instagram 的 Selenium 代码卡在加载图像并停止工作
- java - 如何禁止使用原始类型