Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将python文件保存在特定文件夹中_Python_Python 3.x_Filepath - Fatal编程技术网

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

如何将python文件保存在特定文件夹中,python,python-3.x,filepath,Python,Python 3.x,Filepath,我想将文件保存在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

我想将文件保存在
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>

您需要使用
os.path.join
添加保存文件的完整路径。只需阅读
secure\u filename

 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)