List notebooks¶
from pathlib import Path
This notebook lists all the ipynb files in the parent directory and its subdirectories. The string '..' refers to the parent of the current directory. The function Path('..').glob(p) returns a list of files matching the pattern p. Here we use p='**/*.ipynb'. The double star indicates that the search should reach down into subdirectories and subdirectories of subdirectories and so on, and the suffix '/*.ipynb' indicates that we should only look for files with extension .ipynb, i.e. Jupyter notebooks. Each entry f in the list is actually an instance of the class pathlib.WindowsPath. If we print f itself we get the full file name including the directory path, like ..\notebooks\old\xyz.ipynb. If we instead print f.parts[-1] then we just get the filename, like xyz.ipynb.
n = 0
for f in Path('..').glob('**/*.ipynb'):
print(f.parts[-1])
n += 1
print(f'Total number of notebooks: {n}')