python解压压缩包

def unzip_file(zip_src, dst_dir):
  r = zipfile.is_zipfile(zip_src)
  if r:
    fz = zipfile.ZipFile(zip_src, 'r')
    for file in fz.namelist():
      fz.extract(file, dst_dir)

  else:
    print('This is not zip')

# zip_src: 是zip文件的全路径
# dst_dir: 是要解压到的目的文件夹

2.压缩指定文件夹

def zip_file(src_dir):
  zip_name = src_dir +'.zip'
  z = zipfile.ZipFile(zip_name,'w',zipfile.ZIP_DEFLATED)
  for dirpath, dirnames, filenames in os.walk(src_dir):
    fpath = dirpath.replace(src_dir,'')
    fpath = fpath and fpath + os.sep or ''
    for filename in filenames:
      z.write(os.path.join(dirpath, filename),fpath+filename)
      print ('==压缩成功==')
  z.close()

原文链接:https://blog.csdn.net/weixin_43593330/article/details/103869090

posted @ 2021-04-22 13:03  ArdenWang  阅读(156)  评论(0编辑  收藏  举报