Python uploads files for the first time

Source: Internet
Author: User

Put the first upload zip file, code and error records down:

Requirements: Select a good zip file path in the page, click the Upload button, save the zip file to the project directory, and extract to a folder, delete the zip file, read the extracted file Host.txt file information

Upload file Code reference: Flask Chinese documents, links are as follows: http://docs.jinkan.org/docs/flask/patterns/fileuploads.html

All right, here we go,.

(Below is the full main function)

 import OS 
from flask import flask, request, redirect, Url_for, Render_template
from werkzeug.utils import Secu Re_filename
Import shutil
Import time

Base_dir = Os.path.abspath (Os.path.dirname (__file__)) #获取当前项目路径
Print (base_dir) #此处为验证信息

Allowed_extensions = set ([' Zip ']) #允许文件上传的格式

app = Flask (__name__)
app.config[' upload_folder ' = Base_dir

def allowed_file (filename):
return '. ' in filename and \
Filename.rsplit ('. ', 1) [1] in allowed_extensions

#secure_filename方法解释: If the user uploads a very complex path/file/xxx/mmm/lll/oracle20180903.zip,
You will connect the string to the specified path above. This allows the user to modify a file on the server file system, and he should not have this
kind of permission, so use this function. The
Allowed_file function is used to determine if the uploaded file format is the allowed format

@app. Route ('/', methods=[' GET ', ' POST ')
Def upload_file ():
if Request.method = = ' POST ':
File = request.files[' file ']
If File and Allowed_file (file.filename):
filename = Secure_filename (file.filename)
File.save (Os.path.join (app.config[' upload_folder ', filename))#压缩文件保存在项目路径下
Local_dir = Os.path.join (Base_dir, ' one ')#新创建一个路径, used to put the compressed file
Hh=os.path.join (base_dir,filename)#这个是找到压缩文件路径-------C:/code/haha.zip
Print (HH)
Print (Local_dir)
Shutil.unpack_archive (FILENAME=HH, Extract_dir=local_dir) Save the C:/code/haha.zip in the path you just set
Os.remove (HH)#最后把压缩文件删除
filename = Filename.split ('. ') [0]
Print (filename)#此处为验证信息
Host_path = Os.path.join (local_dir,filename, ' host.txt ')path #host. txt
With open (Host_path, ' R ') as F:#把host文件打开
Key, values = [I.replace (' \ n ', '). Split (', ') for I in F.readlines ()]#列表推倒式, generate a list of keys consisting of a list of values
Hostvalue = dict (Zip (key, values))#把两个列表组成字典
Print (Hostvalue)
ip = hostvalue[' host_os_ip ')#开始读取里面的信息
SystemName = hostvalue[' Host_database_bussines ']
DatabaseName = hostvalue[' host_database_instance ']
Uploadtime = Time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ())
Print (Ip,systemname,databasename,uploadtime)

Return render_template (' notice.html ', filename=filename)
if Request.method = = ' GET ':
Return render_template (' index.html ')




if __name__ = = ' __main__ ':
App.run (Debug=true)

=============================================================================================================== ===============
Record the error message you encountered:
=============================================================================================================== ===============

Because the format that was restricted was zip, but I was preaching rar.

Said did not find this file, because the Host.txt file is not in the 11 folder, 11 under the same time the upload of the name of the compressed package, the approximate path is this: 11/orcl_20180509151053/host.txt

Note: You cannot add 11 directly to filename because the filename is orcl_20180509151053.zip at this point, so first slice it and remove the. zip.

There was also a problem during the run: (probably for PC reasons, not running repeatedly, but always having the interface of the code that ran before, whether the code was changed or before, or even the day before)

My workaround is: cmd to view the process in progress tasklist

And then Tskill python.

And then execute it.

Python uploads files for the first time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.