此错误显示 这是我的代码:
<p>
<a href="{{ url_for('views.download_csv', semester_name=semester.semester_name) }}">Download CSV File</a>
</p>
distribution_df = pd.DataFrame(distribution_dict)
file_name = f'{semester.semester_name}_grade_distributions_{str(datetime.now())}.csv'
file_name = secure_filename(file_name)
output_dir = 'output'
if not os.path.exists(output_dir):
os.makedirs(output_dir)
path = f'{output_dir}/{file_name}'
distribution_df.to_csv(path , index=False)
print("Checking path:", path)
print("Path exists:", os.path.exists(path))
return send_file(path)
那么为什么会出现此错误以及如何修复它。
我尝试在管理员模式下运行我的代码 vscode,也尝试了 send_from_directory 但都不起作用。我希望在我的应用程序中单击链接时下载 csv 文件。
查看其尝试查找的文件的完整路径。在您的错误目录中,由 2 个反斜杠 (\) 分隔,但在代码中构建该路径的最后部分,您使用单个正斜杠 (/)
处理系统路径的更好方法是使用pathlib。
from pathlib import Path
output_path = Path('output') / file_name
您可以像这样修复路径线:
path = f'{output_dir}\\{file_name}'