6.7Python大作业

使用Python实现文件压缩解压工具

【设计要求】创建一个简单的文件压缩工具,可以将文件或文件夹压缩成 zip 格式或rar格式(也可以是其它通用压缩格式)。也可以将压缩文件进行解压。

【界面要求】要求图形界面实现。

压缩文件核心代码:

  1. def zip_file():  
  2.     zip_name = path.get() +'.zip'                           # 获取绝对路径然后给压缩文件加上.zip结尾        
  3.     z = zipfile.ZipFile(zip_name,'w',zipfile.ZIP_DEFLATED)  # 写入.zip  
  4.     for dirpath, dirnames, filenames in os.walk(path.get()):# 使用os遍历目录路径,目录名,文件名  
  5.         fpath = dirpath.replace(path.get(),'')              # 获取目录路径  
  6.         fpath = fpath and fpath + os.sep or ''                
  7.         for filename in filenames:                          # 迭代文件名  
  8.             z.write(os.path.join(dirpath, filename),fpath+filename)  # 写入  
  9.     z.close()                                               # 关闭资源,以免占用内存  
  10. 10.     zip_file_message()                                      # 调用zip_file_message弹出信息框  

解压文件核心代码:

  1. def unzip_file():  
  2.     r = zipfile.is_zipfile(filename.get())          # 判断是否为压缩文件以.zip为判断依据  
  3.     if r:                                           # 如果是则执行if里面的语句,如果不是执行else里面的语句  
  4.         fz = zipfile.ZipFile(filename.get(), 'r')   # 读取压缩文件  
  5.         for file in fz.namelist():                  # 遍历文件  
  6.             fz.extract(file, outputfile.get())      # 输出文件  
  7.         unzip_file_message()                        # 调用unzip_file_message弹出信息框  
  8.     else:  
  9.         failed()                                    # 调用failed弹出信息框  

导入我们需要使用的组件:

  1. from tkinter import *                        # 导入tkinter的所有组件                     
  2. from tkinter.filedialog import askdirectory  # 导入目录操作库  
  3. import tkinter.messagebox                    # 弹出消息框  
  4. import tkinter.filedialog                    # 导入操作文件的库  

界面设计:

root = Tk()              # 初始化

root.mainloop()          # 显示窗口

加入按钮,标签,输入框等等

 

 

 

 

具体代码实现:

  1. from tkinter import *                        # 导入tkinter的所有组件                     
  2. from tkinter.filedialog import askdirectory  # 导入目录操作库  
  3. import tkinter.messagebox                    # 弹出消息框  
  4. import tkinter.filedialog                    # 导入操作文件的库  
  5.   
  6. # 界面代码实现  
  7. def main():  
  8.   
  9.     # label标签 grid括号中的row代表你的label是放在第几行,column是放在第几列  
  10. 10.     Label(root,text = "压缩目录路径:").grid(row = 0, column = 0)  
  11. 11.   
  12. 12.     # Entry是获取输入  
  13. 13.     Entry(root, textvariable = path).grid(row = 0, column = 1)  
  14. 14.   
  15. 15.     # 操作按钮  
  16. 16.     Button(root, text = "目录选择", command = selectPath).grid(row = 0, column = 2)  
  17. 17.   
  18. 18.     # Label标签  
  19. 19.     Label(root,text = "解压目录路径:").grid(row = 1, column = 0)  
  20. 20.      
  21. 21.     # Entry是获取输入  
  22. 22.     Entry(root, textvariable = filename).grid(row = 1, column = 1)  
  23. 23.       
  24. 24.     # 操作按钮  
  25. 25.     Button(root, text = "文件选择", command = selectFile).grid(row = 1, column = 2)  
  26. 26.   
  27. 27.     # Label标签  
  28. 28.     Label(root,text = "解压到:").grid(row = 2, column = 0)  
  29. 29.       
  30. 30.     # Entry是获取输入  
  31. 31.     Entry(root, textvariable = outputfile).grid(row = 2, column = 1)  
  32. 32.       
  33. 33.     # 操作按钮  
  34. 34.     Button(root, text = "解压路径选择", command = outputFlie).grid(row = 2, column = 2)  
  35. 35.   
  36. 36.     # 操作按钮  
  37. 37.     Button(root,text="压缩文件",command=zip_file).grid(row=3,column=0)  
  38. 38.       
  39. 39.     # 操作按钮  
  40. 40.     Button(root,text="解压文件",command=unzip_file).grid(row=3,column=2)  
  41. 41.   
  42. 42.     # 操作按钮  
  43. 43.     Button(root,text="退出程序",command=root.quit).grid(row=4,column=1)  

 

posted @   yblll  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示