[color=green][size=large][b]每到月末,桌面上基本都会爆满,散仙的习惯一个月整理一次桌面,上面放了各种文件,word,excel,txt,jar,压缩包等等,如果每天整理一次,感觉比较麻烦,一个月整理一次,又发现到时候整理的东西,实在太多了,少说也得花了5,6分钟,而且还得,分年分月分类拷贝进去和剪切,所以,就用python写了个整理桌面的小程序,其实不一定是桌面,任何一个路径下的文件,都可以整理,拷贝,整理路径是提前定义好的,这样才能整理。
当然这个程序,现在只是控制台,和cmd或shell下可以运行,等以后有空,把它做成窗体形式这样就更方便了。
先看下整理的几个分类:
[/b][/size][/color]
[img]/upload/attachment/0101/7866/8c9dc8c9-638b-3799-af65-bb6d6e9d1d3c.jpg[/img]
[b][color=olive][size=large]再看下,按年月整理的截图:[/size][/color][/b]
[img]/upload/attachment/0101/7868/bf93169a-98be-3d97-b25e-893e951e6b6e.jpg[/img]
[b][color=olive][size=large]当然,散仙定义的分类不一定非常完全,符合每个人分类习惯,所以会有一个未分类文件夹存放没有被分类的文件,当然如果你想定义的更细分,可以直接在代码里添加一个目录,后面如果有空,等散仙做成窗体的形式,就可以在桌面上动态添加分类文件夹,下面看下源码(基于python3.4的版本):
[/size][/color][/b]
import os import shutil #源目录,这里假设是桌面需要程序自动整理的路径 dirpath="C:\\Users\\qindongliang\\Desktop\\存档\\" #当前月份路径,定义按多少月份归档 currentMonth="9月份整理\\" #根路径,存放一年多个月的归档数据 rootpath="C:\\Users\\qindongliang\\Desktop\\整理\\" #chm文件存档 chm=rootpath+currentMonth+"chm\\" #csv文件存档地址 csv=rootpath+currentMonth+"csv文件\\" #excel文件存档地址 excel=rootpath+currentMonth+"excel\\" #jar文件存档地址 jar=rootpath+currentMonth+"jar包\\" #java相关文件存档地址 java=rootpath+currentMonth+"java的相关\\" #pic相关文件存档地址 pic=rootpath+currentMonth+"pic图片\\" #shell脚本或bat文件相关文件存档地址 shellOrBat=rootpath+currentMonth+"shell脚本或bat文件\\" #txt相关文件存档地址 txt=rootpath+currentMonth+"txt\\" #word相关文件存档地址 word=rootpath+currentMonth+"word\\" #各种文件夹相关文件存档地址 dirss=rootpath+currentMonth+"各种文件夹\\" #tar相关文件存档地址 tar=rootpath+currentMonth+"各种压缩包\\" #pdf相关文件存档地址 pdf=rootpath+currentMonth+"pdf\\" #无分类文件 unknowFile=rootpath+currentMonth+"未分类文件\\" def beginWork(dp): #转成元组表示 tt=tuple(os.walk(dp)); #print(len(tt[0][1])) #一级目录下的所有文件夹 dirs=tt[0][1] #一级目录下的所有文件 files=tt[0][2] print("文件夹总数量: ",len(dirs),"文件总数据量: ",len(files),"总共文件数:",len(dirs)+len(files)) print("====================移动文件开始==================================") for var in tt[0][2]: filename=dp+"\\"+var #print(filename) if filename.endswith("chm") or filename.endswith("chw"): if not os.path.exists(chm): os.makedirs(chm) print(chm,"文件夹不存在,已生成!") shutil.move(filename,chm) elif filename.endswith("jar"): if not os.path.exists(jar): os.makedirs(jar) print(jar,"文件夹不存在,已生成!") shutil.move(filename,jar) elif filename.endswith("csv"): if not os.path.exists(csv): os.makedirs(csv) print(csv,"文件夹不存在,已生成!") shutil.move(filename,csv) elif filename.endswith("excel"): if not os.path.exists(excel): os.makedirs(excel) print(excel,"文件夹不存在,已生成!") shutil.move(filename,excel) elif filename.endswith("java"): if not os.path.exists(java): os.makedirs(java) print(java,"文件夹不存在,已生成!") shutil.move(filename,java) elif filename.endswith("jpg") or filename.endswith("gif"): if not os.path.exists(pic): os.makedirs(pic) print(pic,"文件夹不存在,已生成!") shutil.move(filename,pic) elif filename.endswith("sh") or filename.endswith("bat"): if not os.path.exists(shellOrBat): os.makedirs(shellOrBat) print(shellOrBat,"文件夹不存在,已生成!") shutil.move(filename,shellOrBat) elif filename.endswith("txt"): if not os.path.exists(txt): os.makedirs(txt) print(txt,"文件夹不存在,已生成!") shutil.move(filename,txt) elif filename.endswith("pdf"): if not os.path.exists(pdf): os.makedirs(pdf) print(pdf,"文件夹不存在,已生成!") shutil.move(filename,pdf) elif filename.endswith("doc") or filename.endswith("docx"): if not os.path.exists(word): os.makedirs(word) print(word,"文件夹不存在,已生成!") shutil.move(filename,word) elif filename.endswith("gz") or filename.endswith("zip"): if not os.path.exists(tar): os.makedirs(tar) print(tar,"文件夹不存在,已生成!") shutil.move(filename,tar) else: if not os.path.exists(unknowFile): os.makedirs(unknowFile) print(unknowFile,"文件夹不存在,已生成!") shutil.move(filename,unknowFile) print("无分类的文件: "+filename) #print(filename) print("====================移动目录开始==================================") for var in tt[0][1]: tempdir=dp+"\\"+var shutil.move(tempdir,dirss+"\\"+var) print("移动"+tempdir+"到"+dirss+"路径完毕.......") #开始分类 beginWork(dirpath)
[b][color=olive][size=large]运行效果如下所示:[/size][/color][/b]
文件夹总数量: 27 文件总数据量: 27 总共文件数: 54 ====================移动文件开始================================== C:\Users\qindongliang\Desktop\整理\9月份整理\pic图片\ 文件夹不存在,已生成! C:\Users\qindongliang\Desktop\整理\9月份整理\csv文件\ 文件夹不存在,已生成! C:\Users\qindongliang\Desktop\整理\9月份整理\各种压缩包\ 文件夹不存在,已生成! C:\Users\qindongliang\Desktop\整理\9月份整理\txt\ 文件夹不存在,已生成! C:\Users\qindongliang\Desktop\整理\9月份整理\未分类文件\ 文件夹不存在,已生成! 无分类的文件: C:\Users\qindongliang\Desktop\存档\\ersave.dat C:\Users\qindongliang\Desktop\整理\9月份整理\shell脚本或bat文件\ 文件夹不存在,已生成! C:\Users\qindongliang\Desktop\整理\9月份整理\word\ 文件夹不存在,已生成! 无分类的文件: C:\Users\qindongliang\Desktop\存档\\hadoop-metrics2.properties 无分类的文件: C:\Users\qindongliang\Desktop\存档\\hosts 无分类的文件: C:\Users\qindongliang\Desktop\存档\\hrider.7z C:\Users\qindongliang\Desktop\整理\9月份整理\chm\ 文件夹不存在,已生成! C:\Users\qindongliang\Desktop\整理\9月份整理\jar包\ 文件夹不存在,已生成! C:\Users\qindongliang\Desktop\整理\9月份整理\java的相关\ 文件夹不存在,已生成! C:\Users\qindongliang\Desktop\整理\9月份整理\pdf\ 文件夹不存在,已生成! ====================移动目录开始================================== 移动C:\Users\qindongliang\Desktop\存档\\360wenjianhuifu到C:\Users\qindongliang\Desktop\整理\9月份整理\各种文件夹\路径完毕....... 移动C:\Users\qindongliang\Desktop\存档\\apache-curator-2.6.0-source-release到C:\Users\qindongliang\Desktop\整理\9月份整理\各种文件夹\路径完毕....... 移动C:\Users\qindongliang\Desktop\存档\\bbb到C:\Users\qindongliang\Desktop\整理\9月份整理\各种文件夹\路径完毕....... 移动C:\Users\qindongliang\Desktop\存档\\curatorlib到C:\Users\qindongliang\Desktop\整理\9月份整理\各种文件夹\路径完毕....... 移动C:\Users\qindongliang\Desktop\存档\\data到C:\Users\qindongliang\Desktop\整理\9月份整理\各种文件夹\路径完毕....... 移动C:\Users\qindongliang\Desktop\存档\\dhgate-lganalysis-impl到C:\Users\qindongliang\Desktop\整理\9月份整理\各种文件夹\路径完毕....... 移动C:\Users\qindongliang\Desktop\存档\\easyrecover6.0到C:\Users\qindongliang\Desktop\整理\9月份整理\各种文件夹\路径完毕....... 移动C:\Users\qindongliang\Desktop\存档\\etc到C:\Users\qindongliang\Desktop\整理\9月份整理\各种文件夹\路径完毕....... 移动C:\Users\qindongliang\Desktop\存档\\fdfdfd到C:\Users\qindongliang\Desktop\整理\9月份整理\各种文件夹\路径完毕....... 移动C:\Users\qindongliang\Desktop\存档\\hadoop2.2一键安装到C:\Users\qindongliang\Desktop\整理\9月份整理\各种文件夹\路径完毕....... 移动C:\Users\qindongliang\Desktop\存档\\hadoopconf到C:\Users\qindongliang\Desktop\整理\9月份整理\各种文件夹\路径完毕....... 移动C:\Users\qindongliang\Desktop\存档\\hadoopsys到C:\Users\qindongliang\Desktop\整理\9月份整理\各种文件夹\路径完毕....... 移动C:\Users\qindongliang\Desktop\存档\\hadoop安装脚本到C:\Users\qindongliang\Desktop\整理\9月份整理\各种文件夹\路径完毕....... 移动C:\Users\qindongliang\Desktop\存档\\hbaseconf到C:\Users\qindongliang\Desktop\整理\9月份整理\各种文件夹\路径完毕....... 移动C:\Users\qindongliang\Desktop\存档\\hrider到C:\Users\qindongliang\Desktop\整理\9月份整理\各种文件夹\路径完毕....... 移动C:\Users\qindongliang\Desktop\存档\\lib到C:\Users\qindongliang\Desktop\整理\9月份整理\各种文件夹\路径完毕....... Process finished with exit code -1D:\python\python.exe D:/pythonide/pythonprojectworkspace/python进阶/操作IO/fo.py
[b][color=olive][size=large]完成后,随便打开几个截图如下:[/size][/color][/b]
[img]/upload/attachment/0101/7872/2246d3c5-98f5-3507-ada3-c50263de62da.jpg[/img]
[img]/upload/attachment/0101/7874/29679904-b953-3a3f-bded-54cb80217915.jpg[/img]
[b][color=green][size=large]其实这也没啥技术含量,就是拷贝,目录,判断分类,移动文件或文件夹等,随便用什么语言写都可以完成,最后如果是在桌面运行,可以指定定义一个目录或文件列表,不被移除,比如windows自带的那几个,最好是把所有的杂项文件,放在一个桌面上的文件夹里,这样以来,就可以直接使用了。[/size][/color][/b]