创作灵感
博主本人用电脑时一直有一个不好的习惯,就是喜欢把文件存在放在桌面上,结果没过多久桌面就全是各种word,excel文件了,显得桌面十分的乱,但是又改不掉这个习惯,真是让人脑袋疼!(。>︿<)_θ
电脑桌面就像这样
问题分析
使用python编写一个电脑桌面清理"管家",自动分析桌面文件后缀名。
将桌面上常规的办公文件保存在E盘下的work文件夹下。
将剩余后缀名的文件保存在E盘下的other文件夹下。
【需要提前在E盘下创建work和other这两个文件夹哦!( •̀ ω •́ )✧】
思路讲解
1.定于一个用于获得指定目录下的文件列表的函数
def get_file(self,path=''):
"""
返回指定目录下的文件列表
"""
return os.listdir(path)
2.定义一个用于文件分类的函数
def classification(self,file_list):
"""
检测文件后缀进行分类
"""
for file in file_list:
if ('.' in file):
#检测是否是文件类型
file_name,file_type = file[-5:].split('.')
#分割文件名和文件类型
if file_type in self.sys_type:
#检测是否为系统类型,如果是则跳过
continue
elif file_type in self.work_type:
#检测是否为办公文件类型,如果是则加入work_list
self.work_list.append(file)
else:
#检测是否为其他文件类型,如果是则加入other_list
self.other_list.append(file)
3.定义一个用于补齐文件路径的函数
def move(self):
"""
循环提取文件,然后调用移动函数
"""
if len(self.work_list) > 0:
for file in self.work_list:
print ("正在移动{}".format(file))
src_file = self.desktop_path+'\\'+file
des_file = self.work_path+'\\'+file
self.file_move(src_file,des_file)
self.work_list.remove(file)
elif len(self.other_list) > 0:
for file in self.other_list:
print ('正在移动{}'.format())
src_file = self.desktop_path+'\\'+file
des_file = self.other_path+'\\'+file
self.file_move(src_file,des_file)
self.other_list.remove(file)
else:
pass
4.定义一个用于移动文件的函数
def file_move(self,src_file,des_file):
"""
移动文件
"""
try:
shutil.move(src_file,des_file)
except:
pass
5.定义一个主函数用于调用上述函数
def main(self):
while True:
time.sleep(10)
file_list = self.get_file(self.desktop_path)
self.classification(file_list)
完整的代码
# -*- coding:utf-8 -*-
#creators: 猫先生的早茶
import os
import shutil
import time
class Zeam():
def __init__(self):
self.desktop_path = os.popen(r'echo %userprofile%\desktop').read().rstrip('\n')
self.work_path = r'E:\\work'
self.other_path = r'E:\\other'
self.work_type = ['xlx','xlsx','ppt','pptx','doc','docx','txt']
self.sys_type = ['ini','lnk']
self.work_list =[]
self.other_list = []
def get_file(self,path=''):
"""
返回指定目录下的文件列表
"""
return os.listdir(path)
def classification(self,file_list):
"""
检测文件后缀进行分类
"""
for file in file_list:
if ('.' in file):
#检测是否是文件类型
file_name,file_type = file[-5:].split('.')
#分割文件名和文件类型
if file_type in self.sys_type:
#检测是否为系统类型,如果是则跳过
continue
elif file_type in self.work_type:
#检测是否为办公文件类型,如果是则加入work_list
self.work_list.append(file)
else:
#检测是否为其他文件类型,如果是则加入other_list
self.other_list.append(file)
self.move()
def file_move(self,src_file,des_file):
"""
移动文件
"""
try:
shutil.move(src_file,des_file)
except:
pass
def move(self):
"""
循环提取文件,然后调用移动函数
"""
if len(self.work_list) > 0:
for file in self.work_list:
print ("正在移动{}".format(file))
src_file = self.desktop_path+'\\'+file
des_file = self.work_path+'\\'+file
self.file_move(src_file,des_file)
self.work_list.remove(file)
elif len(self.other_list) > 0:
for file in self.other_list:
print ('正在移动{}'.format())
src_file = self.desktop_path+'\\'+file
des_file = self.other_path+'\\'+file
self.file_move(src_file,des_file)
self.other_list.remove(file)
else:
pass
def main(self):
while True:
time.sleep(10)
file_list = self.get_file(self.desktop_path)
self.classification(file_list)
zeam = Zeam()
zeam.main()
print ("桌面清理管家正在运行。。。")
设置开机运行
运行后的效果图如下:
桌面整洁后,整个人的心情都变好了,今天又是元气满满的一天!( ̄▽ ̄)*
结束语
现在我们自己动手创造了一个桌面清理管家,是不是很棒啊!§( ̄▽ ̄)§傲娇脸!美好的一条从整洁的桌面开始!
源程序下载链接:https://pan.baidu.com/s/1GakILebHnMHvuOl7ipKUWQ
提取码:rr0o