程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python uploads zip attachments and performs decompression

編輯:Python

1、 First, get the uploaded file from the front end

f = request.files['upload']

2、 Configuration save path

savepath = "/opt/upload/"
if not os.path.exists(savepath): # Determine whether the directory exists , If it doesn't exist, create a new one
os.makedirs(savepath)

3、 Save after configuring the path

upload_path = os.path.join(savepath, secure_filename(f.filename))
f.save(upload_path)

4、 Determine whether zip File and extract

if zipfile.is_zipfile(upload_path): # Determine whether zip file
zf = zipfile.ZipFile(upload_path, 'r') # Set the file to readable
stem, suffix = os.path.splitext(f.filename) # Extract file name
for file in zf.namelist(): # Traversal file
zf.extract(file, savepath + "/" + stem) # Extract to the specified directory 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved