Skip to content

Commit db9bb5a

Browse files
committed
feat: add functionality to clean up __pycache__ directories and update README
1 parent 445bb26 commit db9bb5a

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,12 @@ pipx run killpy
5757
- To **mark a virtual environment for deletion**, press `D`.
5858
- To **confirm deletion of marked virtual environments**, press `Ctrl+D`.
5959
- To **delete a virtual environment immediately**, press `Shift+Delete`.
60+
- To **delete a virtual environment immediately**, press `Shift+Delete`.
61+
- To **clean up __pycache__ folders**, press `P`.
6062

6163
## Roadmap
6264

63-
- [ ] Delete `__pycache__` Files
65+
- [x] Delete `__pycache__` Files
6466
- [ ] Remove `dist` Folders and Build Artifacts
6567
- [ ] Clean Up Installed Package Cache
6668
- [ ] Delete `.egg-info` and `.dist-info` Files

killpy/__main__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@
1111
from textual.widgets import DataTable, Footer, Header, Label, Static
1212

1313

14+
def remove_pycache(path: Path) -> int:
15+
total_freed_space = 0
16+
for pycache_dir in path.rglob("__pycache__"):
17+
try:
18+
total_freed_space += get_total_size(pycache_dir)
19+
shutil.rmtree(pycache_dir)
20+
except Exception:
21+
continue
22+
return total_freed_space
23+
24+
1425
def remove_duplicates(venvs):
1526
seen_paths = set()
1627
unique_venvs = []
@@ -156,6 +167,12 @@ class TableApp(App):
156167
description="Delete the selected .venv immediately",
157168
show=True,
158169
),
170+
Binding(
171+
key="p",
172+
action="clean_pycache",
173+
description="Clean __pycache__ directories recursively",
174+
show=True,
175+
),
159176
]
160177

161178
CSS = """
@@ -211,6 +228,13 @@ async def find_venvs(self):
211228

212229
self.query_one(Label).update(f"Found {len(venvs)} .venv directories")
213230

231+
async def action_clean_pycache(self):
232+
current_directory = Path.cwd()
233+
total_freed_space = await asyncio.to_thread(remove_pycache, current_directory)
234+
self.bytes_release += total_freed_space
235+
self.query_one(Label).update(f"{format_size(self.bytes_release)} deleted")
236+
self.bell()
237+
214238
def action_confirm_delete(self):
215239
table = self.query_one(DataTable)
216240
for row_index in range(table.row_count):

0 commit comments

Comments
 (0)