tar压缩文件

#!/usr/bin/env python3   
import os,zipfile,tarfile


def Zip(target_dir):   
    os.chdir(target_dir)
    target_file=raw_input('please input your compress file name: ')
    tar = tarfile.open(target_file, "w")
    while True:
        zip_opt=raw_input("compress all the files in this dir choice y,add files by hand choice n (y/n): ")
        filenames=[]
        if zip_opt=='y':    
            filenames=os.listdir(os.getcwd())
            #tar = tarfile.open(target_file, "w")
            for name in filenames:
                tar.add(name)
            tar.close()
            print("compress finished!")   
            break
        
        elif zip_opt=='n':
            while True:
                aaa=raw_input('input (e) to exit,or input the file name you want compressing : ')
                if aaa=='e':
                    break
                else:
                    filenames.append(aaa)
                    print filenames
                
            for name in filenames:
                tar.add(name)
            tar.close()
            print("compress finished!")   
            break  
        else:   
            print("Please in put the character 'y' or 'n'")   
            #zip_opt=input("Will you zip all the files in this dir?(Choose 'n' you should add files by hand)y/n: ")


def Unzip(target_dir):
    target_name=raw_input("Please input the filename you want uncompress :")
    os.chdir(target_dir)
    tar = tarfile.open(target_name)
    tar.extractall(os.path.join(target_dir,   os.path.splitext(os.path.splitext(target_name)[0])[0]     ))
    tar.close()
    print("Unzip finished!")
   
def main():   
    opt=raw_input("welcome! compress choose 'y',uncompress choose 'n'.y/n: ")   
    while True:   
        if opt=='y':  
            zip_dir=raw_input("Please input the absdir you want put the compress file in:")   
            Zip(zip_dir)   
            break  
        elif opt=='n': 
            unzip_dir=raw_input("Please input the absdir you want put the compress file in or nothing is in the current dir):")
            print unzip_dir
            if unzip_dir=='':   
                Unzip(os.getcwd())   
            else:   
                Unzip(unzip_dir)   
            break  
        else:   
            print("Please input the character 'y' or 'n'")   
            opt=input("What are you gonna do?Zip choose 'y',unzip choose 'n'.y/n: ")   
if __name__=='__main__':   
    main() 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值