Python桌面文件清理脚本分享
作者:小楼夜听雨QAQ 时间:2022-10-11 03:13:23
需求
桌面临时文件较多时,直接删了不太放心,不删又显得很杂乱,故需要写一个脚本批量清理并备份这些鸡肋的文件。
所以脚本需要具有以下功能
1. 可以将桌面文件移动至指定文件夹(可配置)。
2. 可以设置例外文件,比如桌面图标不需要移动,部分常用的文件也不需要移动。
3. 出现同名文件时,不能直接覆盖,需要加一个日期后缀予以区分。例如更名为 helloworld-2022-08-30.txt
本来准备按照文件后缀名分文件夹存放的,但毕竟是临时文件,大概率还是需要定期删除的,分类后反而不利于检索。
实现
目录结构
两个配置文件,一个主类。
代码
ignore.ini配置需要忽略的文件名或者后缀名。
比如需要忽略图标,可以加上.lnk;需要配置忽略文件夹temp,则在尾行加上temp即可;
location.ini配置需要备份至哪个目录
main.py主类
import os
import datetime
import shutil
def get_config(file_name):
"""
读取配置文件
:param file_name: 文件名
:return: 按行读取
"""
f = open(file_name)
lines = []
for line in f.readlines():
line = line.strip('\n')
lines.append(line)
return lines
def get_desktop():
"""
获取桌面路径
:return: 桌面绝对路径
"""
return os.path.join(os.path.expanduser("~"), 'Desktop')
def get_suffix(dir_path):
"""
获取文件的后缀名
:param dir_path: 文件名
:return: 后缀名
"""
return os.path.splitext(dir_path)[-1]
def get_exclude_suffix():
"""
获取不参与整理的文件后缀名
"""
dirs = {}
lines = get_config('ignore.ini')
for line in lines:
dirs.setdefault(line, 0)
return dirs
def get_target_path():
"""
备份至指定文件夹
:return: 目标位置的路径
"""
return get_config('location.ini')[0]
def get_source_dirs():
"""
获取需要转移的文件
:return: 文件目录
"""
dirs = os.listdir(get_desktop())
suffixes = get_exclude_suffix()
fit_dirs = []
for dir in dirs:
suffix = get_suffix(dir)
if suffix not in suffixes and dir not in suffixes:
fit_dirs.append(dir)
return fit_dirs
def get_time():
"""
获取当前年月日
:return: 时间
"""
return datetime.datetime.now().strftime('-%Y-%m-%d')
def get_rename(path):
"""
文件重命名
:param path: 路径
:return: 命名后的路径
"""
if os.path.isdir(path):
return path + get_time()
else:
return os.path.splitext(path)[0] + get_time() + get_suffix(path)
def move():
"""
移动文件
"""
dirs = get_source_dirs()
target_base_path = get_target_path()
desk_url = get_desktop()
if not os.path.exists(target_base_path):
os.makedirs(target_base_path)
for dir in dirs:
path = os.path.join(desk_url, dir)
target_path = os.path.join(target_base_path, dir)
if os.path.exists(target_path):
# 如果有同名文件,则加一个日期后缀
target_path = get_rename(target_path)
shutil.move(path, target_path)
if __name__ == '__main__':
move()
直接 python main.py 执行脚本即可
来源:https://blog.csdn.net/qq_37855749/article/details/126596680
标签:Python,桌面文件,清理
0
投稿
猜你喜欢
python 进程 进程池 进程间通信实现解析
2022-07-05 07:38:28
python搭建微信公众平台
2022-05-24 00:14:09
Oracle 分析函数RANK(),ROW_NUMBER(),LAG()等的使用方法
2009-11-05 21:45:00
Python抓取框架Scrapy爬虫入门:页面提取
2022-04-16 18:57:47
Golang加密解密之RSA(附带php)
2024-05-08 10:21:28
Python 使用PIL numpy 实现拼接图片的示例
2021-03-20 02:50:20
Pandas中GroupBy具体用法详解
2023-08-10 04:16:42
Facebook基础的信息架构图
2008-04-01 09:46:00
jQuery中ajax和post处理json的不同示例对比
2024-04-16 08:54:10
python神经网络编程之手写数字识别
2022-01-12 20:50:04
Navicat for MySQL 与 MySQL-Front比较
2009-02-12 17:33:00
Python二分法搜索算法实例分析
2023-11-01 13:13:15
Python实例解析图像形态学运算技术
2021-04-14 10:34:24
tensorflow 1.X迁移至tensorflow2 的代码写法
2023-07-19 08:30:54
零基础使用Python读写处理Excel表格的方法
2021-01-02 13:33:07
SQL Server误区30日谈 第24天 26个有关还原(Restore)的误区
2024-01-27 19:39:51
Python给你的头像加上圣诞帽
2023-08-28 11:30:41
python3实现ftp服务功能(服务端 For Linux)
2022-05-18 06:05:03
MS SQL2000 数据库自动备份方法
2010-07-22 19:52:00
简述 Python 的类和对象
2023-05-17 12:44:23