Welcome 微信登录

首页 / 操作系统 / Linux / Python 3.2 实现zip压缩与解压缩功能

刚接触Python,用Python3试着写了一个Zip文件的压缩和解压缩脚本,不足之处,欢迎拍砖 
功能有限,要按照脚本压缩文件或者文件夹,必须进入文件所在目录,以文件所在目录为工作路径,并且默认压缩后的文件名同于原文件所在的目录名、解压后的文件夹名同于解压前的文件名 
压缩功能分为压缩所在目录的所有文件跟部分文件(部分文件需要手动添加文件名);解压功能分为解压到当前目录跟解压到指定目录,以下为代码: 
  1. #!/usr/bin/env python3   
  2. import os,zipfile   
  3. def Zip(target_dir):   
  4.     target_file=os.path.basename(os.getcwd())+".zip"  
  5.     zip_opt=input("Will you zip all the files in this dir?(Choose "n" you should add files by hand)y/n: ")   
  6.     while True:   
  7.         if zip_opt=="y":       #compress all the files in this dir   
  8.             filenames=os.listdir(os.getcwd())    #get the file-list of this dir   
  9.             zipfiles=zipfile.ZipFile(os.path.join(target_dir,target_file),"w",compression=zipfile.ZIP_DEFLATED)   
  10.             for files in filenames:   
  11.                 zipfiles.write(files)   
  12.             zipfiles.close()   
  13.             print"Zip finished!")   
  14.             break  
  15.         elif zip_opt=="n":     #compress part of files of this dir   
  16.             filenames=list(input("Please input the files" name you wanna zip:"))   
  17.             zipfiles=zipfile.ZipFile(os.path.join(target_dir,target_file),"w",compression=zipfile.ZIP_DEFLATED)   
  18.             for files in filenames:   
  19.                 zipfiles.write(files)   
  20.             zipfiles.close()   
  21.             print"Zip finished!")   
  22.             break  
  23.         else:   
  24.             print"Please in put the character "y" or "n"")   
  25.             zip_opt=input("Will you zip all the files in this dir?(Choose "n" you should add files by hand)y/n: ")   
  26. def Unzip(target_dir):   
  27.     target_name=input("Please input the file you wanna unzip:")   
  28.     zipfiles=zipfile.ZipFile(target_name,"r")   
  29.     zipfiles.extractall(os.path.join(target_dir,os.path.splitext(target_name)[0]))   
  30.     zipfiles.close()   
  31.     print"Unzip finished!")   
  32. def main():   
  33.     opt=input("What are you gonna do?Zip choose "y",unzip choose "n".y/n: ")   
  34.     while True:   
  35.         if opt=="y":  #compress files   
  36.             zip_dir=input("Please input the absdir you wanna put the zip file in:")   
  37.             Zip(zip_dir)   
  38.             break  
  39.         elif opt=="n":  #unzip files   
  40.             unzip_dir=input("Please input the absdir you wanna put the zip file in(Nothing should be done if you wann unzip files in the current dir):")   
  41.             if unzip_dir=="":   
  42.                 Unzip(os.getcwd())   
  43.             else:   
  44.                 Unzip(unzip_dir)   
  45.             break  
  46.         else:   
  47.             print"Please input the character "y" or "n"")   
  48.             opt=input("What are you gonna do?Zip choose "y",unzip choose "n".y/n: ")   
  49. if __name__=="__main__":   
  50.     main()