Files
2nd/scratch/cleanup.py
T

64 lines
2.0 KiB
Python

import os
import shutil
base_path = r'E:\Wiki\2nd\10_Wiki'
placeholder_text = "I'm now integrating"
deleted_files = []
deleted_dirs = []
# 1. Delete nested directories
nested_dirs = [
os.path.join(base_path, '10_Wiki'),
os.path.join(base_path, '2nd')
]
for d in nested_dirs:
if os.path.exists(d):
try:
shutil.rmtree(d)
deleted_dirs.append(d)
except Exception as e:
print(f"Error deleting dir {d}: {e}")
# 2. Walk through files
for root, dirs, files in os.walk(base_path):
# Delete .obsidian folders found inside
if '.obsidian' in dirs:
obsidian_path = os.path.join(root, '.obsidian')
try:
shutil.rmtree(obsidian_path)
deleted_dirs.append(obsidian_path)
except Exception as e:
print(f"Error deleting .obsidian at {root}: {e}")
for f in files:
if f.endswith('.md'):
file_path = os.path.join(root, f)
# Skip the long path file (User requested to exclude item 4)
if len(file_path) > 240:
print(f"Skipping long path file: {f}")
continue
try:
# 0-byte check
if os.path.getsize(file_path) == 0:
os.remove(file_path)
deleted_files.append(file_path)
continue
# Placeholder content check
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f_obj:
content = f_obj.read(500) # Check first 500 chars
if placeholder_text in content:
os.remove(file_path)
deleted_files.append(file_path)
except Exception as e:
print(f"Error processing file {f}: {e}")
print(f"Total files deleted: {len(deleted_files)}")
print(f"Total dirs deleted: {len(deleted_dirs)}")
for f in deleted_files[:10]:
print(f"Deleted: {os.path.basename(f)}")