Skip to content

Instantly share code, notes, and snippets.

@reddgr
Created March 12, 2025 12:21
Show Gist options
  • Save reddgr/81d13eb3511a6b3f960d4f69a9e73ef1 to your computer and use it in GitHub Desktop.
Save reddgr/81d13eb3511a6b3f960d4f69a9e73ef1 to your computer and use it in GitHub Desktop.
Counting files of a given extension in a given directory and all subdirectories
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total number of .pdf files: 8798\n"
]
}
],
"source": [
"import os\n",
"import time\n",
"from IPython.display import clear_output\n",
"\n",
"def count_files(dir, extension=None, verbose=True):\n",
" total_files = 0\n",
" iteration = 0\n",
" for root, dirs, files in os.walk(dir):\n",
" iteration += 1\n",
" if extension:\n",
" filtered_files = [f for f in files if f.lower().endswith(extension.lower())]\n",
" if verbose and len(filtered_files)>0:\n",
" if iteration % 10 == 0:\n",
" clear_output(wait=True) \n",
" print(f\"{len(filtered_files)} {extension} files in {root}\")\n",
" total_files += len(filtered_files)\n",
" else:\n",
" if verbose: print(f\"{len(files)} files in {root}\")\n",
" total_files += len(files)\n",
"\n",
" return total_files\n",
"\n",
"extension = '.pdf'\n",
"dir='C:/'\n",
"total_files = count_files(dir=dir, extension=extension, verbose=True)\n",
"\n",
"clear_output(wait=True)\n",
"time.sleep(1)\n",
"print(f\"Total number of {extension} files: {total_files}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment