如何在Python中从Flask调用上传的文件名

时间:2019-05-18 20:46:51

标签: python python-3.x flask

我用Flask写了一些代码,允许用户上传一个csv文件。那部分起作用。现在,我要做的就是在以下Python代码中引用该文件名,以便我可以将文件作为数据框上传。

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@app.route('/nlp', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            #return redirect(url_for("upload_file",filename=filename))
            flash('File Uploaded Successfully!')
            return redirect(url_for("upload_file",filename=filename)) 

    return render_template('upload.html')

现在这是Python代码,我只想从上面引用该文件名

file_name= #### the CSV file name the user just uploaded ####    
dirname= 'C:\\upload\\'
df= pd.read_csv(os.path.join(dirname,file_name), header= 0, encoding="cp1252")

0 个答案:

没有答案