我正在构建我的第一个烧瓶应用程序,并且稍后在我的脚本中访问我上传的文件时遇到了问题。我正在使用基本 Flask Upload 脚本的变体,目前我有两种形式,每种形式都采用某种文本文件。我试图稍后在我的脚本中通过一个函数运行输入文件以比较差异。就像现在一样,我收到此错误:
UnboundLocalError: local variable 'filename1' referenced before assignment
如果这是表单问题或保存的文件问题,我正在努力解决问题。这是我的上传文件夹:
UPLOAD_FOLDER = '/uploads'
ALLOWED_EXTENSIONS = set(['doc', 'docx', 'html', 'txt','rtf'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.secret_key = 'MySecretKey'
if not os.path.isdir(UPLOAD_FOLDER):
os.mkdir(UPLOAD_FOLDER)
这是其中一个文件上传,除了文件名之外,另一个几乎相同:
def allowed_audio_file(reference_file):
return '.' in reference_file and \
reference_file.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/referenceupload', methods=['GET', 'POST'])
def upload_reference_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
ref_file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if ref_file.filename == '':
flash('No selected file')
return redirect(request.url)
if ref_file and allowed_audio_file(ref_file.filename):
filename1 = secure_filename(ref_file.filename)
ref_file.save(os.path.join(UPLOAD_FOLDER, filename1))
return redirect(url_for('uploaded_reference_file',
filename1=filename1))
return render_template('reference.html', filename1=filename1)
@app.route('/uploads/<filename1>')
def uploaded_reference_file(filename1):
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename1)
这是完整的错误:
File "C:\Python\Python36\lib\site-packages\flask\app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Python\Python36\lib\site-packages\flask\app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "C:\Python\Python36\lib\site-packages\flask\app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Python\Python36\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Python\Python36\lib\site-packages\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python\Python36\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Python\Python36\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Python\Python36\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Python\Python36\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python\Python36\lib\site-packages\flask\app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\Jesse\eclipse-workspace\WER\src\Word_error_rate\uploads.py", line 55, in upload_reference_file
return render_template('reference.html', filename1=filename1)
UnboundLocalError: local variable 'filename1' referenced before assignment