0

我是烧瓶的新手。我正在尝试进行用户导入并将其保存在后端。我未能保存文件(我看不到静态上传的文件),因此无法从文件中读取。任何人都可以提出解决方案吗?谢谢。

错误:FileNotFoundError:[Errno 2] 没有这样的文件或目录:'static/uploadsDB/Reviews.txt'

我的应用程序.py:

#flask packages
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
from flask_bootstrap import Bootstrap 
from flask_uploads import UploadSet,configure_uploads,IMAGES,DATA,ALL

#systems
import os 
from werkzeug.utils import secure_filename
import spacy

app = Flask(__name__)
Bootstrap(app)
#db = SQLAlchemy(app)

# Configuration for File Uploads
files = UploadSet('files',ALL)
app.config['UPLOADED_FILES_DEST'] = 'static/uploadsDB'
configure_uploads(app,files)

# Spacy packages
nlp = spacy.load("en_core_web_sm")

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/upload', methods=['POST','GET'])
def upload():
    if request.method == 'POST' and 'txt_data' in request.files:
        num_sentences = request.form['num_sentences']
        file = request.files['txt_data']
        filename = secure_filename(file.filename)
        # os.path.join is used so that paths work in every operating system
        # file.save(os.path.join("wherever","you","want",filename))
        file.save(os.path.join('static/uploadsDB',filename))
        fullfile = os.path.join('static/uploadsDB',filename)
        text_file = open(os.path.join('static/uploadsDB',filename), "r").read()
        nlp_file_doc = nlp(text_file)
        sentences = list(nlp_file_doc.sents)
        return render_template('annotation.html', sentences = sentences)

我的 index.html:

<div class="container">
          <div class="main">
              <div class="row">
                <div class="col-md-8" style="background-color:#ecb164">
                  <h3 style="color:#fff">Step 1: Upload your dataset</h3>
                    <p style="color:#fff">This tool only accept <ins>txt</ins> format text data </p>
                    <p style="color:#fff">Every sentence should either separate with punctuation or  </p>
                      <form method=POST enctype=multipart/form-data action="{{ url_for('upload') }}" class="form-group">
                        <div class="form-group">
                          <input type="file" name="txt_data">
                          <h3 style="color:rgb(255, 255, 255)">Step 2: Input lines of sentences you have processed</h3>
                            <p style="color:#fff"> Please enter 0 if this is your first attempt </p>
                        <form method=POST action="{{ url_for('upload') }}" class="form-group">
                        <input type="text" name="num_sentences" value=0>
4

1 回答 1

0

问题是文件一开始没有被保存。

这是有问题的行:

file.save(os.path.join('static/uploadsDB',filename))

在这里,file指的是表单数据,但当您使用Flask-Uploads或其后继Flask-Reuploaded时,UploadSet它负责保存文件。

而你命名你的UploadSet 文件

此外,由于您已经配置了路径来保存文件(app.config['UPLOADED_FILES_DEST'] = 'static/uploadsDB'),您不需要再次重复此信息。

因此,为了保存文件,您只需使用以下代码:

files.save(file)

an的save方法UploadSet也需要一个参数name

如果你想给保存的文件一个任意的名字,你可以写...

files.save(file, name="some_name")

如果您想了解更多信息Flask-Reuploaded,可以随时查看源代码

您还可以在werkzeug 文档中了解有关用于表单结果的数据结构的更多信息。

于 2021-01-18T07:49:29.413 回答