前端人员C盘空间变的太小,怎么办?
使用这个脚本可以很快查出是哪里占用了空间。我查完发现在是YARN 缴存引起的。Yarn 是一个流行的 JavaScript 包管理器,通常与 Node.js 一起使用。在 Windows 系统上,Yarn 通常会在 C:\Users\Administrator\AppData\Local 目录下存储缓存和相关数据。
一般来说,Yarn 的缓存文件夹是可以安全删除的,但这会有一些影响:
可以删除的内容:
Yarn 缓存包文件(位于 Yarn\cache 目录下)
这些文件可以在需要时重新下载
删除的影响:
下次运行 yarn install 时需要重新下载所有依赖包,这可能会花费较长时间
会增加网络流量使用
但不会影响已经安装好的项目运行
建议的操作:
如果磁盘空间紧张,可以删除 Yarn 缓存来释放空间
更推荐的方式是使用 Yarn 自带的清理命令:
yarn cache clean
import os
import sys
from pathlib import Path
def get_directory_size(path):
"""获取目录的总大小(字节)"""
total_size = 0
try:
for dirpath, dirnames, filenames in os.walk(path):
for filename in filenames:
filepath = os.path.join(dirpath, filename)
try:
total_size += os.path.getsize(filepath)
except (OSError, FileNotFoundError):
# 某些系统文件可能无法访问,跳过它们
pass
except PermissionError:
print(f"权限不足,无法访问: {path}")
return total_size
def format_bytes(bytes_value):
"""将字节转换为更易读的格式"""
for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
if bytes_value < 1024.0:
return f"{bytes_value:.2f} {unit}"
bytes_value /= 1024.0
return f"{bytes_value:.2f} PB"
def analyze_disk_usage(drive_path="C:\\Users\\Administrator\\AppData\\Local"):
"""分析磁盘使用情况"""
print(f"正在分析 {drive_path} 盘的使用情况...")
print("-" * 80)
try:
# 获取驱动器根目录下的所有项目
items = os.listdir(drive_path)
except PermissionError:
print(f"权限不足,无法列出 {drive_path} 的内容")
return
folder_sizes = []
for item in items:
item_path = os.path.join(drive_path, item)
# 只处理目录
if os.path.isdir(item_path):
size = get_directory_size(item_path)
folder_sizes.append((item, size))
# 按大小排序(从大到小)
folder_sizes.sort(key=lambda x: x[1], reverse=True)
# 显示结果
total_size = sum(size for _, size in folder_sizes)
print(f"{'目录':<30} {'大小':<15} {'占比'}")
print("-" * 80)
for folder, size in folder_sizes:
if size > 0: # 只显示非空目录
percentage = (size / total_size * 100) if total_size > 0 else 0
print(f"{folder:<30} {format_bytes(size):<15} {percentage:.2f}%")
print("-" * 80)
print(f"{'总计':<30} {format_bytes(total_size):<15} 100.00%")
if __name__ == "__main__":
# 默认分析C盘
drive_to_analyze = "C:\\Users\\Administrator\\AppData\\Local"
# 如果提供了命令行参数,则使用该参数作为驱动器
if len(sys.argv) > 1:
drive_to_analyze = sys.argv[1]
if not drive_to_analyze.endswith("\\"):
drive_to_analyze += "\\"
# 检查驱动器是否存在
if not os.path.exists(drive_to_analyze):
print(f"错误: 驱动器 {drive_to_analyze} 不存在")
sys.exit(1)
analyze_disk_usage(drive_to_analyze)