用户:
小白一号Rua查看:18 回复:5 评论:18 创建时间:2020-11-28T21:43:22
简介:
在生活中,有许多杂乱无章的文件夹需要整理,可整理要花好多时间,这时就要用到文档整理器了!
这个文档整理器可以统计数量,整理子文件夹,按格式整理,按创建日期分类(未完工),将文件剪切(未完工)等。
代码:
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 os.path.splitext(self.save_route)[-1] == '':
break
else:
eg.msgbox('保存的路径文件名不能带有任何后缀哦!!!~','文件整理器','重选')
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.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.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']
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]
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('开始整理!!~')
print('进度:')
self.Progress_bar(0,color='31',unfinished_style='-',bar_len=50)
Classification = self.Classification.currentText()
Date_interva = self.Date_interval.currentText()
os.makedirs(self.SaveRoute.text())
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)
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()
except:
self.hide_all()
self.error.show()
def copy_files(self,Type):
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:
shutil.copy(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_())
效果:


文件过多无响应?

单独文件夹

敬请期待!!!
我的其它作品传送门:https://shequ.codemao.cn/community/341761 【二维码生成器】