Using Python to achieve basic compression function __python

Source: Internet
Author: User
Tags file info
#压缩软件 #设计界面 Import tkinter import tkinter.filedialog import zipfile import os import tkinter.messagebox root = tkinter.t K () Root.title (' Compressed Software 1.0 ') root.minsize (300,400) #设置需要压缩路径变量 zipfilename = [] #添加文件的函数 def addfile (): #全局化变量 glob Al zipfilename #弹出文件选框 files = tkinter.filedialog.askopenfilenames (title = ' Please select the file that needs the sub-cable ') #将选择的文件加入列表中 Zipf Ilename + = list (files) #将信息组成字符串书写 filesstr = ' \ n '. Join (zipfilename) #将文件的信息写入lable显示 lable[' text ' = File SSTR #压缩文件函数 def zip_file (): Path = '/home/caohaifeng/pycharmprojects/text.zip ' #打开或者创建压缩文件 ZP = ZipFile. ZipFile (Path, ' W ') #添加文件 for filename in zipfilename:zp.write (filename,os.path.basename (filename)) #关闭 Compressed file Zp.close () #判断压缩文件是否创建成功 if os.path.exists (path): tkinter.messagebox.showinfo (title = ' Information ', MESSAG E = ' file compression succeeded: ' + path ' else:tkinter.messagebox.showerror (title = ' ERROR ', message = ' compressed file failed.
    ') #解压文件函数 def unzip_file (): #选择需要解压的文件Zipfilepath = Tkinter.filedialog.asksaveasfilename () #选择解压的路径 Unzipfilepath = Tkinter.filedialog.askdirectory () #解压操作 ZP = ZipFile. ZipFile (Zipfilepath) #解压所有 Zp.extractall (Unzipfilepath) #关闭文件 zp.close () #摆放按钮 btn_add = Tkinter. Button (root,text = ' Add file ', command = addfile) btn_add.place (x = 20,y =) Btn_zip = Tkinter. Button (root,text = ' compressed file ', command = zip_file) btn_zip.place (x = 110,y =) Btn_unzip = Tkinter. Button (root,text = ' extract file ', command = unzip_file) btn_unzip.place (x = 200,y =) #显示信息区域 lable = Tkinter. Label (root,text = ' no file info ', bg = ' #abcdef ', anchor = ' NW ', justify = ' left ') lable.place (x =, y = 70,width =, height = 310) Root.mainloop ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.