Python 烧瓶文件操作
我想知道如何在flask中打开、操作和重新保存上传的文件。我有它,所以我可以上传文件,但打开它,做我想做的数据是不会发生的 这是我所拥有的Python 烧瓶文件操作,python,flask,Python,Flask,我想知道如何在flask中打开、操作和重新保存上传的文件。我有它,所以我可以上传文件,但打开它,做我想做的数据是不会发生的 这是我所拥有的 with open(os.path.join(app.config['UPLOAD_FOLDER'], filename), 'r') as f: content = f.read() with open(os.path.join(app.config['UPLOAD_FOLDER'], filename), 'wb') as outputF
with open(os.path.join(app.config['UPLOAD_FOLDER'], filename), 'r') as f:
content = f.read()
with open(os.path.join(app.config['UPLOAD_FOLDER'], filename), 'wb') as outputFile:
outputFile.write(content.lower())
outputFile.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
这是整个app.py文件
import os
from flask import *
from werkzeug import secure_filename
# Initialize the Flask application
app = Flask(__name__)
app.secret_key = '1234'
app.config['UPLOAD_FOLDER'] = 'uploads/'
app.config['ALLOWED_EXTENSIONS'] = set(['txt'])
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']
@app.route('/')
def index():
return render_template('index.html')
# Route that will process the file upload
@app.route('/upload', methods=['POST'])
def upload():
# Get the name of the uploaded file
file = request.files['file']
# Check if the file is one of the allowed types/extensions
if file and allowed_file(file.filename):
# Make the filename safe, remove unsupported chars
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
# Redirect the user to the uploaded_file route, which
# will basicaly show on the browser the uploaded file
with open(os.path.join(app.config['UPLOAD_FOLDER'], filename), 'r') as f:
content = f.read()
with open(os.path.join(app.config['UPLOAD_FOLDER'], filename), 'wb') as outputFile:
outputFile.write(content.lower())
outputFile.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('uploaded_file', filename=filename))
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
if __name__ == '__main__':
app.run(debug=True)
导入操作系统
从烧瓶进口*
从werkzeug导入安全文件名
#初始化烧瓶应用程序
app=烧瓶(名称)
app.secret_key='1234'
app.config['UPLOAD_FOLDER']='uploads/'
app.config['ALLOWED_EXTENSIONS']=set(['txt'])
允许的def_文件(文件名):
在文件名和\
app.config['ALLOWED_EXTENSIONS']中的filename.rsplit('.',1)[1]
@应用程序路径(“/”)
def index():
返回渲染模板('index.html')
#将处理文件上载的路由
@app.route('/upload',methods=['POST'])
def upload():
#获取上载文件的名称
file=request.files['file']
#检查文件是否为允许的类型/扩展名之一
如果文件和允许的文件(file.filename):
#确保文件名安全,删除不支持的字符
filename=secure\u文件名(file.filename)
保存(os.path.join(app.config['UPLOAD\u FOLDER'],文件名))
#将用户重定向到上载的_文件路由,该路由
#基本上我会在浏览器上显示上传的文件吗
将open(os.path.join(app.config['UPLOAD\u FOLDER'],filename),'r')作为f:
content=f.read()
打开(os.path.join(app.config['UPLOAD\u FOLDER',filename),'wb')作为输出文件:
outputFile.write(content.lower())
outputFile.save(os.path.join(app.config['UPLOAD\u FOLDER'],文件名))
返回重定向(url_for('uploaded_file',filename=filename))
@app.route(“/uploads/”)
def上传文件(文件名):
从_目录返回发送_(app.config['UPLOAD_FOLDER'],文件名)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
app.run(debug=True)
提前谢谢 如果嵌套了语句,则需要先关闭在外部语句中打开的文件,然后才能再次打开该文件进行写入:
with open(os.path.join(app.config['UPLOAD_FOLDER'], filename), 'r') as f:
content = f.read()
# At this point, the file is closed
with open(os.path.join(app.config['UPLOAD_FOLDER'], filename), 'wb') as output:
# do stuff
您也不需要保存它,因为一旦执行循环体中的所有语句,循环将关闭(即保存并写入磁盘)文件。谢谢您的回复,我做了更改,但文件仍然没有被操作?对不起,您是对的,我发现了另一个问题-谢谢您的帮助!!太好了,这样别人就知道问题解决了。