为了账号安全,请及时绑定邮箱和手机立即绑定

如何将我的python文件保存在特定文件夹中

如何将我的python文件保存在特定文件夹中

明月笑刀无情 2021-07-16 18:15:07
我想将我的文件保存在image我已经在我的C:/python文件夹中创建的文件夹中。此代码将我的文件保存在Python文件夹中:from flask import Flask, render_template, requestfrom werkzeug import secure_filenameapp = Flask(__name__)@app.route('/upload')def load_file():return render_template('upload.html')@app.route('/uploader', methods = ['GET', 'POST'])def upload_file():if request.method == 'POST':   f = request.files['file']   f.save(secure_filename(f.filename))   return 'file uploaded successfully' if __name__ == '__main__':  app.run(debug = True)html代码  <form action = "http://localhost:5000/uploader" method = "POST"      enctype = "multipart/form-data">     <input type = "file" name = "file" />     <input type = "submit"/>  </form>
查看完整描述

2 回答

?
紫衣仙女

TA贡献1839条经验 获得超15个赞

 from flask import Flask, render_template, request

 from werkzeug import secure_filename


 UPLOAD_FOLDER = '/path/to/the/uploads'

 ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])


 app = Flask(__name__)

 app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

 import os, os.path



 APP_ROOT = os.path.dirname(os.path.abspath(__file__))

 UPLOAD_FOLD = '/python/image/'

 UPLOAD_FOLDER = os.path.join(APP_ROOT, UPLOAD_FOLD)

 app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER




@app.route('/upload')

def load_file():

return render_template('upload.html')


@app.route('/uploader', methods = ['GET', 'POST'])

def upload_file():

if request.method == 'POST':

  f = request.files['file']   

  f.save(os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(f.filename)))

  return 'file uploaded successfully'


if __name__ == '__main__':

app.run(debug = True)


查看完整回答
反对 回复 2021-07-21
  • 2 回答
  • 0 关注
  • 443 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信