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

我想将我的文件保存在image我已经在我的C:/python文件夹中创建的文件夹中。此代码将我的文件保存在Python文件夹中:


from flask import Flask, render_template, request

from werkzeug import secure_filename

app = 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>


明月笑刀无情
浏览 444回答 2
2回答

紫衣仙女

&nbsp;from flask import Flask, render_template, request&nbsp;from werkzeug import secure_filename&nbsp;UPLOAD_FOLDER = '/path/to/the/uploads'&nbsp;ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])&nbsp;app = Flask(__name__)&nbsp;app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER&nbsp;import os, os.path&nbsp;APP_ROOT = os.path.dirname(os.path.abspath(__file__))&nbsp;UPLOAD_FOLD = '/python/image/'&nbsp;UPLOAD_FOLDER = os.path.join(APP_ROOT, UPLOAD_FOLD)&nbsp;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':&nbsp; f = request.files['file']&nbsp; &nbsp;&nbsp; f.save(os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(f.filename)))&nbsp; return 'file uploaded successfully'if __name__ == '__main__':app.run(debug = True)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python