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