小编Fab*_*ima的帖子

Python 和 Flask - 尝试让函数返回文件内容

我正在努力将文件内容返回给用户。有一个从用户那里接收 txt 文件的 Flask 代码,然后调用 Python 函数 transform() 来解析 infile,这两个代码都在做这项工作。

当我尝试将新文件 (outfile) 发送(返回)给用户时,问题发生了,用于该文件的 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 …
Run Code Online (Sandbox Code Playgroud)

python return file flask

2
推荐指数
1
解决办法
5132
查看次数

标签 统计

file ×1

flask ×1

python ×1

return ×1