-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlist_notebooks.py
More file actions
38 lines (31 loc) · 1.22 KB
/
list_notebooks.py
File metadata and controls
38 lines (31 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import os
from datetime import datetime
def list_notebooks(root='.',extension='.ipynb'):
"""
% (C) Nick Holschuh - Amherst College -- 2022 ([email protected])
%
% This function takes a directory and finds all python notebooks in subdirectories
% sorted by modified date.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
"""
notebooks = []
for dirpath, _, filenames in os.walk(root):
for file in filenames:
if file.endswith(extension):
full_path = os.path.join(dirpath, file)
mtime = os.path.getmtime(full_path)
notebooks.append({
'filename': file,
'directory': dirpath,
'modified': datetime.fromtimestamp(mtime)
})
# Sort by last modified date, descending
notebooks.sort(key=lambda x: x['modified'], reverse=True)
# Print header
print(f"{'Notebook Name':40} {'Directory':40} {'Last Modified':25}")
print("-"*105)
# Print each notebook
for nb in notebooks:
print(f"{nb['filename'][:39]:40} {nb['directory'][:39]:40} {nb['modified'].strftime('%Y-%m-%d %H:%M:%S'):25}")
return notebooks