python中将zip压缩包转为gz.tar的方法

作者:晓东邪 时间:2022-02-28 18:50:49 

由于同事电脑上没有直接可以压缩gz.tar格式的压缩软件,而工作中这个又时常需要将zip文件转换为gz.tar格式,所以常常将压缩为zip格式的文件发给我来重新压缩成gz.tar格式发给他,能偷懒就不想动手,就用python的tarfile和zipfile包完成了一个将zip转换成gz.tar格式的小脚本:

代码比较简单,也就几行,但是写的时候因为绝对路径的问题浪费了点时间,代码水平还是有待提高。


#coding: utf-8

import os
import tarfile
import zipfile

def zip2tar(root_path, name,to_name='test'):

'''
root_path: 压缩文件所在根目录
name: 压缩文件名字(zip格式)
'''
#root_path = r'C:\Users\Administrator\Desktop\somefiles'
#file_path = os.path.join(root_path, 'somemodel.zip')

file_path = os.path.join(root_path, name+'.zip')

with zipfile.ZipFile(file_path, 'r') as zzip:
 with tarfile.open(os.path.join(root_path, to_name+'.gz.tar'), 'w') as ttar:
  for ffile in zzip.namelist():
   if not os.path.isdir(ffile):
   #if not ffile.strip().endswith(r'/'):
    zzip.extract(ffile, root_path)
    ttar.add(os.path.join(root_path,ffile), arcname=ffile)

if __name__ == '__main__':

root_path = raw_input(u'input root path: ')
name = raw_input(u'input the zip name(without .zip): ')
zip2tar(root_path, name)

来源:https://blog.csdn.net/xiaodongxiexie/article/details/71483693

标签:python,zip,gz.tar
0
投稿

猜你喜欢

  • Dreamweaver如何制作会移动的广告条

    2010-10-20 20:04:00
  • Python接口自动化之浅析requests模块post请求

    2022-12-10 21:10:05
  • 一文详解Go语言fmt标准库的常用占位符使用

    2023-08-07 01:57:56
  • Python正则表达式使用经典实例

    2022-04-29 01:22:03
  • python TK库简单应用(实时显示子进程输出)

    2023-10-08 23:08:19
  • Python基于回溯法子集树模板实现8皇后问题

    2023-09-25 08:34:45
  • 使用Python批量压缩tif文件操作步骤

    2021-03-27 00:04:10
  • Linux(Redhat)安装python3.6虚拟环境(推荐)

    2022-06-08 23:34:13
  • python 使用pygame工具包实现贪吃蛇游戏(多彩版)

    2021-05-10 17:29:19
  • opencv实现图像旋转效果

    2023-07-17 13:28:40
  • CREATE FUNCTION sqlserver用户定义函数

    2012-08-21 10:57:42
  • Python中无限循环需要什么条件

    2023-03-28 09:05:14
  • 一文带你搞懂Python中的pyc文件

    2022-01-25 20:01:03
  • 交互设计实用指南系列(3)—“有效性”之“适时帮助”

    2009-12-25 14:29:00
  • Python实现我的世界小游戏源代码

    2023-07-26 02:09:08
  • 有效LOGO设计的最重要的提示

    2010-06-09 12:05:00
  • numpy数组之存取文件的实现示例

    2021-02-20 11:32:57
  • python3字符串输出常见面试题总结

    2021-01-13 08:39:23
  • Firefox 3.5 新增加的支持(整理)

    2009-08-01 12:51:00
  • python 批量将PPT导出成图片集的案例

    2021-09-14 17:52:36
  • asp之家 网络编程 m.aspxhome.com