用户:
小白一号Rua查看:31 回复:5 评论:31 创建时间:2020-12-02T22:01:51
我把文档整理器的所有功能都做完了!
部分BUG请谅解
文档整理器系列 - 传送门:
https://shequ.codemao.cn/community/351868 【半成品-无素材版】
https://shequ.codemao.cn/community/351743 【半成品效果】
代码:
import sys
import os
import glob
import easygui as eg
import time
import shutil
from PySide2.QtWidgets import *
from PySide2.QtGui import *
from ui_file_organizer import Ui_MainWindow
class Filter(QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
print('请不要关掉我!我在UI无响应时可以帮你查看进度!')
self.setupUi(self)
self.show()
self.Reset_Status_indicator()
self.button()
self.init()
def init(self):
self.route = ''
self.save_route = ''
self.num = 0
self.other_file = []
def button(self):
self.reset_Status_indicator.clicked.connect(self.Reset_Status_indicator)
self.Chose_route.clicked.connect(lambda:self.get_Route('read'))
self.Chose_SaveRoute_.clicked.connect(lambda:self.get_Route('save'))
self.reset.clicked.connect(self.get_file_num)
self.run.clicked.connect(self.try_to_Organiz)
def get_Route(self,pattern):
if pattern == 'read':
last_route = self.route
self.route = eg.diropenbox()
else:
last_route = self.save_route
while True:
self.save_route = eg.filesavebox('选择处理保存路径')
if self.save_route != None:
if os.path.splitext(self.save_route)[-1] == '':
break
else:
eg.msgbox('保存的路径文件名不能带有任何后缀哦!!!~','文件整理器','重选')
else:
break
if self.route == None and pattern == 'read':
if self.Route.text() != '未选择...':
if eg.ynbox('你尚未选择文件夹,是否清空内容?','文件整理器',('清空','保留')) == False:
self.Route.setText(last_route)
self.route = last_route
self.get_file_num()
else:
self.Route.setText('未选择...')
else:
self.Route.setText('未选择...')
elif self.save_route == None and pattern == 'save':
if self.Route.text() != '未选择...':
if eg.ynbox('你尚未选择文件夹,是否清空内容?','文件整理器',('清空','保留')) == False:
self.SaveRoute.setText(last_route)
self.save_route = last_route
self.get_file_num()
else:
self.SaveRoute.setText('未选择...')
else:
self.Route.setText('未选择...')
elif self.save_route != None and pattern == 'save':
self.SaveRoute.setText(self.save_route)
self.get_file_num()
elif self.route != None and pattern == 'read':
self.Route.setText(self.route)
self.get_file_num()
def get_file_num(self):
if self.route != '' and self.route != None:
self.read_other_files()
if 'True' in self.Organize_subfolders.currentText():
self.png.display(len(glob.glob(f'{self.route}\**/*.png',recursive=True)))
self.jpg.display(len(glob.glob(f'{self.route}\**/*.jpg',recursive=True)))
self.gif.display(len(glob.glob(f'{self.route}\**/*.gif',recursive=True)))
self.bmp.display(len(glob.glob(f'{self.route}\**/*.bmp',recursive=True)))
self.ico.display(len(glob.glob(f'{self.route}\**/*.ico',recursive=True)))
self.docx.display(len(glob.glob(f'{self.route}\**/*.docx',recursive=True)))
self.xlsx.display(len(glob.glob(f'{self.route}\**/*.xlsx',recursive=True)))
self.pptx.display(len(glob.glob(f'{self.route}\**/*.pptx',recursive=True)))
self.pdf.display(len(glob.glob(f'{self.route}\**/*.pdf',recursive=True)))
self.txt.display(len(glob.glob(f'{self.route}\**/*.txt',recursive=True)))
self.json.display(len(glob.glob(f'{self.route}\**/*.json',recursive=True)))
self.py.display(len(glob.glob(f'{self.route}\**/*.py',recursive=True)))
self.rar.display(len(glob.glob(f'{self.route}\**/*.rar',recursive=True)))
self.Zip.display(len(glob.glob(f'{self.route}\**/*.zip',recursive=True)))
self.mp3.display(len(glob.glob(f'{self.route}\**/*.mp3',recursive=True)))
self.wav.display(len(glob.glob(f'{self.route}\**/*.wav',recursive=True)))
self.mp4.display(len(glob.glob(f'{self.route}\**/*.mp4',recursive=True)))
self.ui.display(len(glob.glob(f'{self.route}\**/*.ui',recursive=True)))
self.exe.display(len(glob.glob(f'{self.route}\**/*.exe',recursive=True)))
self.html.display(len(glob.glob(f'{self.route}\**/*.html',recursive=True)))
self.lnk.display(len(glob.glob(f'{self.route}\**/*.lnk',recursive=True)))
self.All.display(len(glob.glob(f'{self.route}\**/*.*',recursive=True)))
else:
self.png.display(len(glob.glob(f'{self.route}\*.png')))
self.jpg.display(len(glob.glob(f'{self.route}\*.jpg')))
self.gif.display(len(glob.glob(f'{self.route}\*.gif')))
self.bmp.display(len(glob.glob(f'{self.route}\*.bmp')))
self.ico.display(len(glob.glob(f'{self.route}\*.ico')))
self.docx.display(len(glob.glob(f'{self.route}\*.docx')))
self.xlsx.display(len(glob.glob(f'{self.route}\*.xlsx')))
self.pptx.display(len(glob.glob(f'{self.route}\*.pptx')))
self.pdf.display(len(glob.glob(f'{self.route}\*.pdf')))
self.txt.display(len(glob.glob(f'{self.route}\*.txt')))
self.json.display(len(glob.glob(f'{self.route}\*.json')))
self.py.display(len(glob.glob(f'{self.route}\*.py')))
self.rar.display(len(glob.glob(f'{self.route}\*.rar')))
self.Zip.display(len(glob.glob(f'{self.route}\*.zip')))
self.mp3.display(len(glob.glob(f'{self.route}\*.mp3')))
self.wav.display(len(glob.glob(f'{self.route}\*.wav')))
self.mp4.display(len(glob.glob(f'{self.route}\*.mp4')))
self.ui.display(len(glob.glob(f'{self.route}\*.ui')))
self.exe.display(len(glob.glob(f'{self.route}\*.exe')))
self.html.display(len(glob.glob(f'{self.route}\*.html')))
self.lnk.display(len(glob.glob(f'{self.route}\*.lnk')))
self.All.display(len(glob.glob(f'{self.route}\*.*')))
self.other.display(len(self.other_file))
else:
eg.msgbox('你还尚未选择读取路径哦!~','Error:未设置读取路径','去设置')
self.get_Route('read')
def read_other_files(self):
self.new_file_object = []
self.other_file = []
self.know_file_object = ['.png','.jpg','.gif','.bmp','.docx','.xlsx','.pptx','.pdf','.txt','.json','.py','.rar','.zip','.wav','.mp3','.mp4','.ui','.exe','.html','.lnk']
self.Object = [self.png,self.jpg,self.gif,self.bmp,self.docx,self.xlsx,self.pptx,self.pdf,self.txt,self.json,self.py,self.rar,self.Zip,self.wav,self.mp3,self.mp4,self.ui,self.exe,self.html,self.lnk]
if 'True' in self.Organize_subfolders.currentText():
All = glob.glob(f'{self.route}\**/*.*', recursive=True)
else:
All = glob.glob(f'{self.route}\*.*')
for i in All:
tmp = os.path.splitext(i)[1]
if tmp not in self.know_file_object:
file_object_name = tmp[1:len(tmp)]
if file_object_name not in self.new_file_object:
self.new_file_object.append(file_object_name)
self.other_file.append(i)
def Progress_bar(self,speed_of_progress,color='37',finished_style='█',unfinished_style=' ',wait_time=0.3,bar_len=100):
Percentage_pe_grid = 100 // bar_len
self.num += 1
if self.num >= Percentage_pe_grid:
self.num = 0
Lattice = speed_of_progress // Percentage_pe_grid
s1 = "\r[%s%s]%d%%"%(finished_style*Lattice,unfinished_style*(bar_len-Lattice),speed_of_progress)
else:
Lattice = int(speed_of_progress) // int(Percentage_pe_grid)
s1 = "\r[%s%s]%d%%"%(finished_style*Lattice,unfinished_style*(bar_len-Lattice),speed_of_progress)
sys.stdout.write(f'\033[0;{color};{40}m\t{s1}\033[0m')
sys.stdout.flush()
time.sleep(int(wait_time))
def try_to_Organiz(self):
if self.save_route == '' or self.save_route == None:
self.hide_all()
self.error.show()
eg.msgbox('你还尚未选择保存路径哦!~','Error:未设置保存路径','去设置')
self.get_Route('save')
return
if self.route == '' or self.route == None:
self.hide_all()
self.error.show()
eg.msgbox('你还尚未选择读取路径哦!~','Error:未设置读取路径','去设置')
self.get_Route('read')
return
# try:
self.hide_all()
self.runing.show()
print('\n')
print('开始整理!!~')
print('进度:')
self.Progress_bar(0,color='31',unfinished_style='-',bar_len=50)
Classification = self.Classification.currentText()
os.makedirs(self.SaveRoute.text())
if Classification == '按创建日期分类':
En_to_int = {'Jan':'1','Feb':'2','Mar':'3','Apr':'4','May':'5','Jun':'6','Jul':'7','Aug':'8','Sep':'9','Oct':'10','Nov':'11','Dec':'12'}
Date_interva = self.Date_interval.currentText()
self.dates = {}
if 'True' in self.Organize_subfolders.currentText():
All = glob.glob(f'{self.route}\**/*.*',recursive=True)
else:
All = glob.glob(f'{self.route}\*.*')
for Route in All:
if self.Date_interval.currentText() == '1年':
ctime = time.ctime(os.path.getctime(Route))[20:24]+'年'
elif self.Date_interval.currentText() == '1个月':
tmp = time.ctime(os.path.getctime(Route))
ctime = tmp[20:24]+'年'+En_to_int[tmp[4:7]]+'月'
if ctime in self.dates:
self.dates[ctime].append(Route)
else:
self.dates[ctime] = [Route]
if Classification == '按格式分类':
num = 0
for i in range(len(self.know_file_object)):
if self.Object[i].value() > 0:
self.copy_files(self.know_file_object[i][1:len(self.know_file_object[i])])
num += 100 // len(self.know_file_object)
if num <= 30:
self.Progress_bar(num,color='31',unfinished_style='-',bar_len=50)
elif num >30 and num <= 70:
self.Progress_bar(num,color='33',unfinished_style='-',bar_len=50)
else:
self.Progress_bar(num,color='32',unfinished_style='-',bar_len=50)
time.sleep(0.15)
self.Progress_bar(95, color='32', unfinished_style='-', bar_len=50)
if 'True' in self.Organize_other.currentText():
if self.other_file != []:
for i in self.new_file_object:
self.copy_files(i)
self.Progress_bar(100, color='32', unfinished_style='-', bar_len=50)
self.hide_all()
self.ok.show()
elif Classification == '按创建日期分类':
num = 0
for date in self.dates:
for Route in self.dates[date]:
self.copy_files(date,Route)
num += 100 // len(self.dates)
if num <= 30:
self.Progress_bar(num,color='31',unfinished_style='-',bar_len=50)
elif num >30 and num <= 70:
self.Progress_bar(num,color='33',unfinished_style='-',bar_len=50)
else:
self.Progress_bar(num,color='32',unfinished_style='-',bar_len=50)
time.sleep(0.15)
self.Progress_bar(100, color='32', unfinished_style='-', bar_len=50)
self.hide_all()
self.ok.show()
# except:
# self.hide_all()
# self.error.show()
def copy_files(self,Type,Route=None):
if '年' in Type:
try:
os.makedirs(self.SaveRoute.text()+f'\\{Type}')
except:
pass
if '推荐' in self.Arrangement_method.currentText():
print()
shutil.copy(Route,self.SaveRoute.text()+f'\\{Type}')
else:
shutil.move(Route,self.SaveRoute.text()+f'\\{Type}')
else:
if Type == 'lnk':
os.makedirs(self.SaveRoute.text()+f'\\快捷方式')
else:
os.makedirs(self.SaveRoute.text()+f'\\{Type}')
if 'True' in self.Organize_subfolders.currentText():
List = glob.glob(f'{self.route}\**/*.{Type}', recursive=True)
else:
List = glob.glob(f'{self.route}/*.{Type}')
for route in List:
if '推荐' in self.Arrangement_method.currentText():
shutil.copy(route, self.SaveRoute.text()+f'\\{Type}')
else:
shutil.move(route,self.SaveRoute.text()+f'\\{Type}')
def hide_all(self):
self.wait.hide()
self.runing.hide()
self.ok.hide()
self.error.hide()
def Reset_Status_indicator(self):
self.hide_all()
self.wait.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Filter()
sys.exit(app.exec_
效果:

拿【编程猫】为例,可以看到,分类准确无误,是2020年8月创建的
二:
文件夹a是有东西的

运行后文件从a被剪切到c
这就是本次完善的内容了
【求助】哪位大佬有py转exe,求在评论区中留下方法
其它作品: