【发布时间】:2015-06-02 03:00:39
【问题描述】:
我正在努力将文件内容返回给用户。有一个从用户接收 txt 文件的 Flask 代码,然后调用 Python 函数 transform() 来解析 infile,这两个代码都在做这项工作。
当我尝试将新文件(输出文件)发送(返回)给用户时,问题正在发生,Flask 代码也可以正常工作。 但我不知道如何让这个 Python 转换函数()“返回”该文件内容,已经测试了几个选项。
以下详细信息:
def transform(filename):
with open(os.path.join(app.config['UPLOAD_FOLDER'],filename), "r") as infile:
with open(os.path.join(app.config['UPLOAD_FOLDER'], 'file_parsed_1st.txt'), "w") as file_parsed_1st:
p = CiscoConfParse(infile)
'''
parsing the file uploaded by the user and
generating the result in a new file(file_parsed_1st.txt)
that is working OK
'''
with open (os.path.join(app.config['UPLOAD_FOLDER'], 'file_parsed_1st.txt'), "r") as file_parsed_2nd:
with open(os.path.join(app.config['UPLOAD_FOLDER'], 'file_parsed_2nd.txt'), "w") as outfile:
'''
file_parsed_1st.txt is a temp file, then it creates a new file (file_parsed_2nd.txt)
That part is also working OK, the new file (file_parsed_2nd.txt)
has the results I want after all the parsing;
Now I want this new file(file_parsed_2nd.txt) to "return" to the user
'''
#Editing -
#Here is where I was having a hard time, and that now is Working OK
#using the follwing line:
return send_file(os.path.join(app.config['UPLOAD_FOLDER'], 'file_parsed_2nd.txt'))
【问题讨论】: