我让神经网络写了一个本地网络传输文件的代码,但是它报错,我不明白这是为什么。
仅在查看可供安装的文件列表时出现错误。
from flask import Flask, request, redirect, url_for, send_from_directory
import os
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'C:\\Users\\KoBaNkas\\Desktop\\files\\uploads'
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
file = request.files['file']
filename = file.filename
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('uploaded_file', filename=filename))
return '''
<html>
<body>
<h1>Upload File</h1>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
<br>
<a href="/list">List Files</a>
</body>
</html>
'''
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return f'File {filename} was uploaded successfully'
@app.route('/list')
def list_files():
file_list = os.listdir(app.config['UPLOAD_FOLDER'])
return '''
<html>
<body>
<h1>Files</h1>
<ul>
{% for file in files %}
<li><a href="{{ url_for('download_file', filename=file) }}">{{ file }}</a></li>
{% endfor %}
</ul>
</body>
</html>
'''.format(files=file_list)
@app.route('/uploads/download/<filename>')
def download_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
if __name__ == '__main__':
# Listen on all available network interfaces, on port 5000
app.run(host='0.0.0.0', port=5000, debug=True)
# Print the URL the server is running on, for convenience
ip = os.environ.get('WERKZEUG_SERVER_IP', '127.0.0.1')
port = int(os.environ.get('WERKZEUG_SERVER_PORT', '5000'))
print(f'Server running at http://{ip}:{port}/')
我尝试用谷歌搜索并将路径粘贴到包含文件的文件夹,但显然我的知识还不够。