2026/4/18 15:35:44
网站建设
项目流程
网站建设用那个软件,电脑和手机都能浏览的网站开发,提高工作效率的措施,大数据抓取客户软件前提条件回收站清理需要安装 winshell 库命令#xff1a;pip install winshell 执行命令#xff1a;将代码保存为xx.py文件 找到脚本文件所在目录#xff0c;执行命令#xff1a;python xx.py清理的文件夹和文件类型1. 下载文件夹 (cleanup_downloads())
- 位置#xff1a…前提条件回收站清理需要安装 winshell 库命令pip install winshell执行命令将代码保存为xx.py文件 找到脚本文件所在目录执行命令python xx.py清理的文件夹和文件类型1. 下载文件夹 (cleanup_downloads())- 位置用户下载文件夹 Downloads 或 下载 - 清理对象- 临时文件 *.tmp , *.temp- 日志文件 *.log- 备份文件 *.bak- 浏览器下载临时文件 *.crdownload , *.partial , *.download2. ModelScope 缓存 (cleanup_modelscope_cache())- 位置- ~/.cache/modelscope 主要缓存目录- ~/AppData/Local/modelscope- ~/.modelscope- 清理对象- 已下载的模型文件如 Wan2.2-T2V-A14B 模型- 所有 ModelScope SDK 缓存的文件3. Python 缓存 (cleanup_python_cache())- 位置- pip 缓存 ~/AppData/Local/pip/cache- conda 缓存如果安装了 conda ~/AppData/Local/conda/conda/pkgs- 清理对象- Python 包安装缓存4. 系统临时文件 (cleanup_temp_files())- 位置系统临时文件夹 tempfile.gettempdir() - 清理对象所有临时文件和文件夹5. 用户临时文件 (cleanup_user_temp_files())- 位置 ~/AppData/Local/Temp- 清理对象用户级别的临时文件和文件夹6. 浏览器缓存 (cleanup_browser_cache())- 位置- Chrome 浏览器缓存 ~/AppData/Local/Google/Chrome/User Data/Default/Cache- Edge 浏览器缓存 ~/AppData/Local/Microsoft/Edge/User Data/Default/Cache- 清理对象浏览器缓存文件7. Windows 更新缓存 (cleanup_windows_update_cache())- 位置 C:\Windows\SoftwareDistribution\Download- 清理对象Windows 系统更新过程中下载的临时文件8. Windows 错误报告文件 (cleanup_error_reports())- 位置 ~/AppData/Local/Microsoft/Windows/WER- 清理对象系统错误报告文件9. 系统日志文件 (cleanup_system_logs())- 位置 C:\Windows\System32\winevt\Logs- 清理对象Windows 系统事件日志文件 *.evtx 10. 回收站 (cleanup_recycle_bin())- 需要安装 winshell 库- 清理对象回收站中的所有文件示例代码#!/usr/bin/env python3# -*- coding: utf-8 -*-C盘空间清理脚本用于清理下载文件夹、缓存文件夹、已下载的模型及垃圾文件import osimport shutilimport sysimport tempfileimport globdef get_user_profile():获取当前用户的Profile目录if sys.platform.startswith(win32):return os.environ.get(USERPROFILE, os.path.expanduser(~))return os.path.expanduser(~)def cleanup_downloads():清理下载文件夹print(清理下载文件夹...)try:# 用户下载文件夹download_dirs [os.path.join(get_user_profile(), Downloads),os.path.join(get_user_profile(), 下载)]for download_dir in download_dirs:if os.path.exists(download_dir):print(f检查文件夹: {download_dir})# 可以添加要清理的特定文件类型extensions_to_remove [*.tmp, *.temp, *.log, *.bak,*.crdownload, *.partial, *.download]for ext in extensions_to_remove:pattern os.path.join(download_dir, ext)for file_path in glob.glob(pattern):try:os.remove(file_path)print(f删除临时文件: {os.path.basename(file_path)})except Exception as e:print(f无法删除 {file_path}: {e})return Trueexcept Exception as e:print(f清理下载文件夹失败: {e})return Falsedef cleanup_modelscope_cache():清理ModelScope缓存print(\n清理ModelScope缓存...)try:cache_dirs [os.path.join(get_user_profile(), .cache, modelscope),os.path.join(get_user_profile(), AppData, Local, modelscope),os.path.join(get_user_profile(), .modelscope)]removed_size 0for cache_dir in cache_dirs:if os.path.exists(cache_dir):print(f检查ModelScope缓存: {cache_dir})# 获取缓存目录大小total_size 0for dirpath, dirnames, filenames in os.walk(cache_dir):for f in filenames:fp os.path.join(dirpath, f)total_size os.path.getsize(fp)if total_size 0:try:shutil.rmtree(cache_dir)removed_size total_sizeprint(f删除了 {format_size(total_size)} 的ModelScope缓存)except Exception as e:print(f无法删除ModelScope缓存 {cache_dir}: {e})return removed_sizeexcept Exception as e:print(f清理ModelScope缓存失败: {e})return 0def cleanup_python_cache():清理Python缓存print(\n清理Python缓存...)try:removed_size 0# 清理pip缓存pip_cache_dir os.path.join(get_user_profile(), AppData, Local, pip, cache)if os.path.exists(pip_cache_dir):total_size get_dir_size(pip_cache_dir)shutil.rmtree(pip_cache_dir)removed_size total_sizeprint(f删除了 {format_size(total_size)} 的pip缓存)# 清理conda缓存如果安装了condaconda_cache_dir os.path.join(get_user_profile(), AppData, Local, conda, conda, pkgs)if os.path.exists(conda_cache_dir):total_size get_dir_size(conda_cache_dir)shutil.rmtree(conda_cache_dir)removed_size total_sizeprint(f删除了 {format_size(total_size)} 的conda缓存)return removed_sizeexcept Exception as e:print(f清理Python缓存失败: {e})return 0def cleanup_temp_files():清理临时文件print(\n清理临时文件...)try:removed_size 0# 系统临时文件夹temp_dir tempfile.gettempdir()if os.path.exists(temp_dir):temp_files glob.glob(os.path.join(temp_dir, *))for temp_file in temp_files:try:if os.path.isfile(temp_file):file_size os.path.getsize(temp_file)os.remove(temp_file)removed_size file_sizeelif os.path.isdir(temp_file):dir_size get_dir_size(temp_file)shutil.rmtree(temp_file)removed_size dir_sizeexcept Exception:pass # 忽略无法删除的临时文件print(f删除了 {format_size(removed_size)} 的临时文件)return removed_sizeexcept Exception as e:print(f清理临时文件失败: {e})return 0def cleanup_browser_cache():清理浏览器缓存print(\n清理浏览器缓存...)try:removed_size 0# Chrome浏览器缓存chrome_cache_dir os.path.join(get_user_profile(), AppData, Local, Google, Chrome, User Data, Default, Cache)if os.path.exists(chrome_cache_dir):try:size get_dir_size(chrome_cache_dir)shutil.rmtree(chrome_cache_dir)os.mkdir(chrome_cache_dir)removed_size sizeprint(f删除了 {format_size(size)} 的Chrome浏览器缓存)except Exception as e:print(f无法清理Chrome浏览器缓存: {e})# Edge浏览器缓存edge_cache_dir os.path.join(get_user_profile(), AppData, Local, Microsoft, Edge, User Data, Default, Cache)if os.path.exists(edge_cache_dir):try:size get_dir_size(edge_cache_dir)shutil.rmtree(edge_cache_dir)os.mkdir(edge_cache_dir)removed_size sizeprint(f删除了 {format_size(size)} 的Edge浏览器缓存)except Exception as e:print(f无法清理Edge浏览器缓存: {e})return removed_sizeexcept Exception as e:print(f清理浏览器缓存失败: {e})return 0def get_dir_size(dir_path):获取目录大小total_size 0try:for dirpath, dirnames, filenames in os.walk(dir_path):for f in filenames:fp os.path.join(dirpath, f)if os.path.exists(fp):total_size os.path.getsize(fp)except Exception:passreturn total_sizedef format_size(size_bytes):格式化文件大小if size_bytes 0:return 0 Bytesunits [B, KB, MB, GB, TB]i 0while size_bytes 1024 and i len(units)-1:size_bytes / 1024.0i 1return f{size_bytes:.1f} {units[i]}def cleanup_windows_update_cache():清理Windows更新缓存print(\n清理Windows更新缓存...)try:removed_size 0# Windows更新缓存目录update_cache_dir os.path.join(os.environ.get(WINDIR, C:\\Windows), SoftwareDistribution, Download)if os.path.exists(update_cache_dir):total_size get_dir_size(update_cache_dir)if total_size 0:try:shutil.rmtree(update_cache_dir)os.mkdir(update_cache_dir)removed_size total_sizeprint(f删除了 {format_size(total_size)} 的Windows更新缓存)except Exception as e:print(f无法清理Windows更新缓存: {e})return removed_sizeexcept Exception as e:print(f清理Windows更新缓存失败: {e})return 0def cleanup_recycle_bin():清理回收站需要管理员权限print(\n清理回收站...)try:import winshellif winshell.recycle_bin().count() 0:# 获取回收站大小total_size 0for item in winshell.recycle_bin():total_size item.size# 清空回收站winshell.recycle_bin().empty(confirmFalse, show_progressFalse, soundFalse)print(f删除了 {format_size(total_size)} 的回收站内容)return total_sizeelse:print(回收站为空)return 0except ImportError:print(未找到 winshell 库无法清理回收站)return 0except Exception as e:print(f清理回收站失败: {e})return 0def cleanup_system_logs():清理系统日志文件print(\n清理系统日志文件...)try:removed_size 0# Windows系统日志目录log_dir os.path.join(os.environ.get(WINDIR, C:\\Windows), System32, winevt, Logs)if os.path.exists(log_dir):log_files glob.glob(os.path.join(log_dir, *.evtx))for log_file in log_files:try:file_size os.path.getsize(log_file)# 系统日志文件通常需要管理员权限才能删除这里尝试重命名后删除temp_name log_file .oldos.rename(log_file, temp_name)os.remove(temp_name)removed_size file_sizeprint(f删除了日志文件: {os.path.basename(log_file)} ({format_size(file_size)}))except Exception as e:print(f无法删除日志文件 {os.path.basename(log_file)}: {e})return removed_sizeexcept Exception as e:print(f清理系统日志文件失败: {e})return 0def cleanup_error_reports():清理Windows错误报告文件print(\n清理Windows错误报告文件...)try:removed_size 0# Windows错误报告目录error_report_dir os.path.join(get_user_profile(), AppData, Local, Microsoft, Windows, WER)if os.path.exists(error_report_dir):for root, dirs, files in os.walk(error_report_dir):for file in files:file_path os.path.join(root, file)try:file_size os.path.getsize(file_path)os.remove(file_path)removed_size file_sizeexcept Exception:passfor dir_name in dirs:dir_path os.path.join(root, dir_name)try:dir_size get_dir_size(dir_path)shutil.rmtree(dir_path)removed_size dir_sizeexcept Exception:passif removed_size 0:print(f删除了 {format_size(removed_size)} 的Windows错误报告文件)else:print(未找到可清理的错误报告文件)return removed_sizeexcept Exception as e:print(f清理Windows错误报告文件失败: {e})return 0def cleanup_user_temp_files():清理用户临时文件目录print(\n清理用户临时文件目录...)try:removed_size 0# 用户临时文件夹user_temp_dir os.path.join(get_user_profile(), AppData, Local, Temp)if os.path.exists(user_temp_dir):temp_files glob.glob(os.path.join(user_temp_dir, *))for temp_file in temp_files:try:if os.path.isfile(temp_file):file_size os.path.getsize(temp_file)os.remove(temp_file)removed_size file_sizeelif os.path.isdir(temp_file):dir_size get_dir_size(temp_file)shutil.rmtree(temp_file)removed_size dir_sizeexcept Exception:passprint(f删除了 {format_size(removed_size)} 的用户临时文件)return removed_sizeexcept Exception as e:print(f清理用户临时文件目录失败: {e})return 0def main():print( * 60)print(C盘空间清理脚本)print( * 60)total_removed 0# 清理下载文件夹if cleanup_downloads():passelse:print(下载文件夹清理失败)# 清理ModelScope缓存modelscope_size cleanup_modelscope_cache()total_removed modelscope_size# 清理Python缓存python_cache_size cleanup_python_cache()total_removed python_cache_size# 清理临时文件temp_file_size cleanup_temp_files()total_removed temp_file_size# 清理用户临时文件目录user_temp_size cleanup_user_temp_files()total_removed user_temp_size# 清理浏览器缓存browser_cache_size cleanup_browser_cache()total_removed browser_cache_size# 清理Windows更新缓存windows_update_size cleanup_windows_update_cache()total_removed windows_update_size# 清理Windows错误报告文件error_report_size cleanup_error_reports()total_removed error_report_size# 清理系统日志文件system_log_size cleanup_system_logs()total_removed system_log_size# 清理回收站需要管理员权限recycle_bin_size cleanup_recycle_bin()total_removed recycle_bin_sizeprint(\n * 60)print(清理完成)print( * 60)print(f总共释放空间: {format_size(total_removed)})print(\n建议)print(- 定期运行此清理脚本)print(- 删除不再需要的大型安装文件)print(- 清理系统更新遗留文件)print(- 检查并清理回收站)print(- 禁用不需要的启动程序)return 0if __name__ __main__:try:exit_code main()sys.exit(exit_code)except KeyboardInterrupt:print(\n\n✗ 清理操作被用户中断)sys.exit(1)