Python3 compress the files in the folder to zip

From , 3 Years ago, written in Python, viewed 197 times.
URL https://pastebin.vip/view/a64a034c
  1. '''
  2. Created on Dec 24, 2012
  3. 将文件归档到zip文件,并从zip文件中读取数据
  4. @author: liury_lab
  5. '''
  6.  
  7. # 压缩成zip文件
  8. from zipfile import *   #@UnusedWildImport
  9. import os
  10.  
  11. my_dir = 'd:/中华十大名帖/'
  12. myzip = ZipFile('d:/中华十大名帖.zip', 'w', ZIP_DEFLATED)
  13. for file_name in os.listdir(my_dir):
  14.     file_path = my_dir + file_name
  15.     print(file_path)
  16.     myzip.write(file_path)
  17. myzip.close()
  18.  
  19. print('finished')
  20.  
  21. # 从zip 文件中读取数据
  22. # 直接检查一个zip格式的归档文件中部分或所有的文件,同时还要避免将这些文件展开到磁盘上
  23. my_zip = ZipFile('d:/中华十大名帖.zip')
  24. for file_name in my_zip.namelist():
  25.     print('File:', file_name, end = ' ')
  26.     file_bytes = my_zip.read(file_name)
  27.     print('has ', len(file_bytes), ' bytes')
  28.  
  29. #//python/6048

Reply to "Python3 compress the files in the folder to zip"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website