一个检查文件夹下文件大小的小程序
2016-04-11 17:31
447 查看
#!/usr/bin/env python3 # -*- coding:utf-8 -*- "选择盘符或选择文件夹,在桌面生成Log文件,将选择目录下文件按由大到小排列" import os from tkinter import * from tkinter.ttk import * from tkinter.filedialog import * import threading filelist = [] def formatSize(size, is1024=True): "格式化文件大小" formatUnit = {1024: ['KB', 'MB', 'GB', 'TB'], 1000: ['KiB', 'MiB', 'GiB', 'TiB']} block = 1024 if is1024 else 1000 for unit in formatUnit[block]: size /= block if size < block: return "{0:.2f}{1}".format(size, unit) raise ValueError('文件过大') def scanDir(beginPath='E:\\'): # print('Current Path: {}'.format(beginPath)) try: for item in os.scandir(beginPath): if item.is_file(): path = os.path.join(beginPath, item.name) # print('当前文件: {}'.format(path)) size = item.stat().st_size filelist.append([path, size]) else: # pass path = os.path.join(beginPath, item.name) # print("目录: {}".format(path)) scanDir(path) except PermissionError: print('访问拒绝:{}'.format(beginPath)) def outPut(logPath=os.path.join(os.path.expanduser('~'),'Desktop','log.txt')): newlist = sorted(filelist, key=lambda x:x[1], reverse=True) with open(logPath,'w') as f: f.write("{:>6} {}\n".format("大小","路径")) for file in newlist: f.write("{:>10} {}\n".format(formatSize(file[1]),file[0])) def test(): scanDir() outPut() def main(): def func(getpath): scanDir(getpath) outPut() print('文件搜索完成') def showMsg(*args): basePath = {"C": "C:\\", "D": "D:\\", "E": "E:\\", "F": "F:\\"} root.update() threading.Thread(target=func,args=(basePath[path.get()],)).start() return def go(*args): threading.Thread(target=func, args=(enter.get(),)).start() return def search(*args): enter.set(askdirectory()) root = Tk() root.title("文件大小查看") path = StringVar() selectPath = Combobox(root, textvariable=path, values=["C","D","E","F"]) selectPath.current(0) checkbtn = Button(root,text="Go",command=go) enter = StringVar() enterPath = Button(root, textvariable=enter, command=search) enter.set("请选择一个文件夹") selectPath.bind("<<ComboboxSelected>>", showMsg) selectPath.pack(fill=X) enterPath.pack(side="left") checkbtn.pack(fill=X) root.mainloop() if __name__=="__main__": main()
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法