我想创建一个简单的Flask应用程序,上传.txt文件,并显示其中的字母数。
我尝试了本教程,没有成功: http://flask.pocoo.org/docs/0.10/patterns/fileuploads/
我的部分代码:
@app.route('/uploads/<filename>')
def uploaded_file(filename):
f=send_from_directory(app.config['UPLOAD_FOLDER'],
filename)
cout= Counter(letter for line in f
for letter in line.lower()
if letter in ascii_lowercase)
return cout
我的日志:
类型错误 TypeError:&#39;响应&#39;对象不可迭代
答案 0 :(得分:1)
send_from_directory
是提供文件的快捷方式。我假设您要做的是读取您之前上传的文件。
import os
@app.route('/uploads/<filename>')
def uploaded_file(filename):
with open(os.path.join(app.config['UPLOAD_FOLDER'], filename)) as f:
cout = Counter(letter for line in f
for letter in line.lower()
if letter in str.ascii_lowercase)
return cout