Folder and File Search in Python¶
This notebook serves as a practical introduction to Python file-system navigation, file searching, directory traversal, and folder filtering using both os and pathlib. It introduces several Python concepts, libraries, classes, methods, and functions.
Major Topics Covered¶
- Finding a specific file recursively
- Searching for files by extension
- Listing folder contents
- Listing immediate subfolders
- Recursive folder traversal
- Filtering files and folders
- Excluding selected folders from searches
- Using
osversuspathlib - Building file paths
- Numbering search results
- Case-insensitive file and folder matching
Recursively search for "FolderFileSearch.ipynb" and print its full path - Method 1¶
In [158]:
from pathlib import Path
root = Path(r"C:\Explore")
for file in root.rglob("*.ipynb"):
if file.name.lower() == "foldersfilessearch.ipynb":
print(file)
C:\Explore\Python\FoldersFilesSearch.ipynb
Recursively search for "FolderFileSearch.ipynb" and print its full path - Method 2¶
In [163]:
import os
root = r"C:\Explore"
for foldername, subfolders, filenames in os.walk(root):
if "FoldersFilesSearch.ipynb" in filenames:
print(os.path.join(foldername, "FoldersFilesSearch.ipynb"))
C:\Explore\Python\FoldersFilesSearch.ipynb
Print full paths of the first-level subfolders (non-recursively) - Method 1¶
In [134]:
# Print full paths of the first-level subfolders (non-recursively) - Method 1
import os
folder = r"C:\Explore"
for item in sorted(os.listdir(folder)):
full_path = os.path.join(folder, item)
if os.path.isdir(full_path):
print(full_path)
C:\Explore\BDMDataTables C:\Explore\DataHive C:\Explore\Extra_SUDAAN C:\Explore\HTML C:\Explore\LabPythonPGMS C:\Explore\Obsolete_pgms C:\Explore\Python C:\Explore\PythonBDM_YouTube C:\Explore\PythonScriptsBDM C:\Explore\Python_Code_BangIndia C:\Explore\SAS C:\Explore\SUDAAN C:\Explore\r-basics C:\Explore\stata-complex-surveys
Print full paths of the first-level subfolders (non-recursively) - Method 2¶
In [108]:
# Print full paths of the first-level subfolders (non-recursively) - Method 2
from pathlib import Path
folder = Path(r"C:\Explore")
for item in sorted(folder.iterdir()):
if item.is_dir():
print(item)
C:\Explore\BDMDataTables C:\Explore\DataHive C:\Explore\Extra_SUDAAN C:\Explore\HTML C:\Explore\LabPythonPGMS C:\Explore\Obsolete_pgms C:\Explore\Python C:\Explore\Python_Code_BangIndia C:\Explore\PythonBDM_YouTube C:\Explore\PythonScriptsBDM C:\Explore\r-basics C:\Explore\SAS C:\Explore\stata-complex-surveys C:\Explore\SUDAAN
In [170]:
import os
root = r"C:\Explore"
exclude = {
"anaconda_projects",
".virtual_documents",
".ipynb_checkpoints",
".git",
"Obsolete_pgms",
"YouTubeApp_Obsoletes",
"Obsolete_SAS_Pgms",
"Obsolete_output",
"Obsolete_YT_Python_Pgms",
"Microsoft.Flow",
"__pycache__"
}
exclude = {x.lower() for x in exclude}
for foldername, subfolders, filenames in os.walk(root):
subfolders[:] = [
d for d in subfolders
if d.lower() not in exclude
]
print(foldername)
C:\Explore C:\Explore\BDMDataTables C:\Explore\BDMDataTables\Charts C:\Explore\BDMDataTables\CSVText C:\Explore\BDMDataTables\ExcelData C:\Explore\BDMDataTables\HTMLOutput C:\Explore\BDMDataTables\PythonPgms C:\Explore\BDMDataTables\SASData C:\Explore\BDMDataTables\SASOutput C:\Explore\BDMDataTables\SASPgms C:\Explore\DataHive C:\Explore\DataHive\BDIndiaCensusHistorical C:\Explore\DataHive\BDIndiaCensusHistorical\Charts C:\Explore\DataHive\BDViolenceAgainstMinority C:\Explore\DataHive\BDViolenceAgainstMinority\BDViolenceAM_Charts C:\Explore\DataHive\BDViolenceAgainstMinority\BDViolenceAM_Reports C:\Explore\DataHive\BDViolenceAgainstMinority\BDViolenceAM_VideoResources C:\Explore\DataHive\BDViolenceAgainstMinority\DataTables C:\Explore\DataHive\BDViolenceAgainstMinority\DataTables\2023AndEarlier C:\Explore\DataHive\BDViolenceAgainstMinority\DataTables\2024 C:\Explore\DataHive\BDViolenceAgainstMinority\DataTables\2025 C:\Explore\DataHive\BDViolenceAgainstMinority\DataTables\2026 C:\Explore\DataHive\BDViolenceAgainstMinority\GoogleNotebookLM_BDViolenceAM C:\Explore\Extra_SUDAAN C:\Explore\HTML C:\Explore\LabPythonPGMS C:\Explore\Python C:\Explore\Python\Extra_Pgms C:\Explore\Python\Extra_Pgms\code_bootcamp_jupyter_nb C:\Explore\Python\Extra_Pgms\code_by_pkmedu C:\Explore\Python\Extra_Pgms\Lectures C:\Explore\Python\Extra_Pgms\Postgres C:\Explore\Python\Extra_Pgms\PythonScriptsBDM C:\Explore\Python\Extra_Pgms\PythonScripts_Transcripts C:\Explore\Python\Extra_Pgms\PythonScripts_Transcripts\Data C:\Explore\Python\Extra_Pgms\PythonScripts_Transcripts\Data\YouTubeFlowExport C:\Explore\PythonBDM_YouTube C:\Explore\PythonBDM_YouTube\Original_YT_PGMS C:\Explore\PythonBDM_YouTube\Output C:\Explore\PythonBDM_YouTube\Output\Year2024 C:\Explore\PythonBDM_YouTube\Output\Year2025 C:\Explore\PythonBDM_YouTube\Output\Year2026 C:\Explore\PythonBDM_YouTube\PythonOutput C:\Explore\PythonBDM_YouTube\PythonPgms C:\Explore\PythonScriptsBDM C:\Explore\Python_Code_BangIndia C:\Explore\Python_Code_BangIndia\Census_BD_India C:\Explore\Python_Code_BangIndia\charts C:\Explore\Python_Code_BangIndia\Obsolete_BD_India_PyPgms C:\Explore\r-basics C:\Explore\r-basics\BuiltInData C:\Explore\r-basics\Data C:\Explore\r-basics\dput_dget C:\Explore\r-basics\ManipulatingData C:\Explore\r-basics\Misc_R_Code C:\Explore\r-basics\ReadingDataIntoR C:\Explore\r-basics\Resources C:\Explore\r-basics\R_Language_Concepts C:\Explore\r-basics\SASUsingRmakrdown C:\Explore\r-basics\SimulateData C:\Explore\r-basics\UtilityTools C:\Explore\r-basics\WorkingWithVectors C:\Explore\SAS C:\Explore\SAS\Lesson1 C:\Explore\SAS\Lesson1\Lesson1data C:\Explore\SAS\Lesson1\SAS_Codes C:\Explore\SAS\Lesson10 C:\Explore\SAS\Lesson10\SAS_Codes C:\Explore\SAS\Lesson11 C:\Explore\SAS\Lesson11\Compiled_Macros C:\Explore\SAS\Lesson11\Data C:\Explore\SAS\Lesson11\DO_Over_Compiled C:\Explore\SAS\Lesson11\SAS_Codes C:\Explore\SAS\Lesson12 C:\Explore\SAS\Lesson12\SAS_Codes C:\Explore\SAS\Lesson13 C:\Explore\SAS\Lesson13\Lesson13data C:\Explore\SAS\Lesson13\SAS_Codes C:\Explore\SAS\Lesson14 C:\Explore\SAS\Lesson14\SAS_Codes C:\Explore\SAS\Lesson15 C:\Explore\SAS\Lesson16 C:\Explore\SAS\Lesson17 C:\Explore\SAS\Lesson2 C:\Explore\SAS\Lesson2\Lesson2Data C:\Explore\SAS\Lesson2\SAS_Codes C:\Explore\SAS\Lesson3 C:\Explore\SAS\Lesson3\Lesson3Data C:\Explore\SAS\Lesson3\SAS_Codes C:\Explore\SAS\Lesson4 C:\Explore\SAS\Lesson4\SAS_Codes C:\Explore\SAS\Lesson5 C:\Explore\SAS\Lesson5\SAS_Codes C:\Explore\SAS\Lesson6 C:\Explore\SAS\Lesson6\SAS_Codes C:\Explore\SAS\Lesson7 C:\Explore\SAS\Lesson7\Lesson7Data C:\Explore\SAS\Lesson7\SAS_Codes C:\Explore\SAS\Lesson8 C:\Explore\SAS\Lesson8\SAS_Codes C:\Explore\SAS\Lesson9 C:\Explore\SAS\Lesson9\SAS_Codes C:\Explore\stata-complex-surveys C:\Explore\SUDAAN
In [144]:
import os
root = r"C:\Explore"
exclude = {
"anaconda_projects",
".virtual_documents",
".ipynb_checkpoints",
".git",
"Obsolete_pgms",
"YouTubeApp_Obsoletes",
"Obsolete_SAS_Pgms",
"Obsolete_output",
"Obsolete_YT_Python_Pgms",
"Microsoft.Flow",
"__pycache__"
}
exclude = {x.lower() for x in exclude}
count = 0
for foldername, subfolders, filenames in os.walk(root):
subfolders[:] = [
d for d in subfolders
if d.lower() not in exclude
]
count += 1
print(f"{count:3d}. {foldername}")
1. C:\Explore 2. C:\Explore\BDMDataTables 3. C:\Explore\BDMDataTables\Charts 4. C:\Explore\BDMDataTables\CSVText 5. C:\Explore\BDMDataTables\ExcelData 6. C:\Explore\BDMDataTables\HTMLOutput 7. C:\Explore\BDMDataTables\PythonPgms 8. C:\Explore\BDMDataTables\SASData 9. C:\Explore\BDMDataTables\SASOutput 10. C:\Explore\BDMDataTables\SASPgms 11. C:\Explore\DataHive 12. C:\Explore\DataHive\BDIndiaCensusHistorical 13. C:\Explore\DataHive\BDIndiaCensusHistorical\Charts 14. C:\Explore\DataHive\BDViolenceAgainstMinority 15. C:\Explore\DataHive\BDViolenceAgainstMinority\BDViolenceAM_Charts 16. C:\Explore\DataHive\BDViolenceAgainstMinority\BDViolenceAM_Reports 17. C:\Explore\DataHive\BDViolenceAgainstMinority\BDViolenceAM_VideoResources 18. C:\Explore\DataHive\BDViolenceAgainstMinority\DataTables 19. C:\Explore\DataHive\BDViolenceAgainstMinority\DataTables\2023AndEarlier 20. C:\Explore\DataHive\BDViolenceAgainstMinority\DataTables\2024 21. C:\Explore\DataHive\BDViolenceAgainstMinority\DataTables\2025 22. C:\Explore\DataHive\BDViolenceAgainstMinority\DataTables\2026 23. C:\Explore\DataHive\BDViolenceAgainstMinority\GoogleNotebookLM_BDViolenceAM 24. C:\Explore\Extra_SUDAAN 25. C:\Explore\HTML 26. C:\Explore\LabPythonPGMS 27. C:\Explore\Python 28. C:\Explore\Python\Extra_Pgms 29. C:\Explore\Python\Extra_Pgms\code_bootcamp_jupyter_nb 30. C:\Explore\Python\Extra_Pgms\code_by_pkmedu 31. C:\Explore\Python\Extra_Pgms\Lectures 32. C:\Explore\Python\Extra_Pgms\Postgres 33. C:\Explore\Python\Extra_Pgms\PythonScriptsBDM 34. C:\Explore\Python\Extra_Pgms\PythonScripts_Transcripts 35. C:\Explore\Python\Extra_Pgms\PythonScripts_Transcripts\Data 36. C:\Explore\Python\Extra_Pgms\PythonScripts_Transcripts\Data\YouTubeFlowExport 37. C:\Explore\PythonBDM_YouTube 38. C:\Explore\PythonBDM_YouTube\Original_YT_PGMS 39. C:\Explore\PythonBDM_YouTube\Output 40. C:\Explore\PythonBDM_YouTube\Output\Year2024 41. C:\Explore\PythonBDM_YouTube\Output\Year2025 42. C:\Explore\PythonBDM_YouTube\Output\Year2026 43. C:\Explore\PythonBDM_YouTube\PythonOutput 44. C:\Explore\PythonBDM_YouTube\PythonPgms 45. C:\Explore\PythonScriptsBDM 46. C:\Explore\Python_Code_BangIndia 47. C:\Explore\Python_Code_BangIndia\Census_BD_India 48. C:\Explore\Python_Code_BangIndia\charts 49. C:\Explore\Python_Code_BangIndia\Obsolete_BD_India_PyPgms 50. C:\Explore\r-basics 51. C:\Explore\r-basics\BuiltInData 52. C:\Explore\r-basics\Data 53. C:\Explore\r-basics\dput_dget 54. C:\Explore\r-basics\ManipulatingData 55. C:\Explore\r-basics\Misc_R_Code 56. C:\Explore\r-basics\ReadingDataIntoR 57. C:\Explore\r-basics\Resources 58. C:\Explore\r-basics\R_Language_Concepts 59. C:\Explore\r-basics\SASUsingRmakrdown 60. C:\Explore\r-basics\SimulateData 61. C:\Explore\r-basics\UtilityTools 62. C:\Explore\r-basics\WorkingWithVectors 63. C:\Explore\SAS 64. C:\Explore\SAS\Lesson1 65. C:\Explore\SAS\Lesson1\Lesson1data 66. C:\Explore\SAS\Lesson1\SAS_Codes 67. C:\Explore\SAS\Lesson10 68. C:\Explore\SAS\Lesson10\SAS_Codes 69. C:\Explore\SAS\Lesson11 70. C:\Explore\SAS\Lesson11\Compiled_Macros 71. C:\Explore\SAS\Lesson11\Data 72. C:\Explore\SAS\Lesson11\DO_Over_Compiled 73. C:\Explore\SAS\Lesson11\SAS_Codes 74. C:\Explore\SAS\Lesson12 75. C:\Explore\SAS\Lesson12\SAS_Codes 76. C:\Explore\SAS\Lesson13 77. C:\Explore\SAS\Lesson13\Lesson13data 78. C:\Explore\SAS\Lesson13\SAS_Codes 79. C:\Explore\SAS\Lesson14 80. C:\Explore\SAS\Lesson14\SAS_Codes 81. C:\Explore\SAS\Lesson15 82. C:\Explore\SAS\Lesson16 83. C:\Explore\SAS\Lesson17 84. C:\Explore\SAS\Lesson2 85. C:\Explore\SAS\Lesson2\Lesson2Data 86. C:\Explore\SAS\Lesson2\SAS_Codes 87. C:\Explore\SAS\Lesson3 88. C:\Explore\SAS\Lesson3\Lesson3Data 89. C:\Explore\SAS\Lesson3\SAS_Codes 90. C:\Explore\SAS\Lesson4 91. C:\Explore\SAS\Lesson4\SAS_Codes 92. C:\Explore\SAS\Lesson5 93. C:\Explore\SAS\Lesson5\SAS_Codes 94. C:\Explore\SAS\Lesson6 95. C:\Explore\SAS\Lesson6\SAS_Codes 96. C:\Explore\SAS\Lesson7 97. C:\Explore\SAS\Lesson7\Lesson7Data 98. C:\Explore\SAS\Lesson7\SAS_Codes 99. C:\Explore\SAS\Lesson8 100. C:\Explore\SAS\Lesson8\SAS_Codes 101. C:\Explore\SAS\Lesson9 102. C:\Explore\SAS\Lesson9\SAS_Codes 103. C:\Explore\stata-complex-surveys 104. C:\Explore\SUDAAN
Distinguish files from folders¶
In [148]:
import os
folder = r"C:\Explore\r-basics"
print("Contents of folder:")
for item in sorted(os.listdir(folder)):
full_path = os.path.join(folder, item)
if os.path.isdir(full_path):
print(f"[DIR] {item}")
else:
print(f"[FILE] {item}")
Contents of folder: [DIR] .git [DIR] BuiltInData [DIR] Data [FILE] DataManipulation.R [FILE] DatasetsInRPackages.R [FILE] LICENSE [DIR] ManipulatingData [DIR] Misc_R_Code [DIR] R_Language_Concepts [DIR] ReadingDataIntoR [FILE] ReadingWritingDataFiles.R [DIR] Resources [DIR] SASUsingRmakrdown [FILE] Simple_index.html [DIR] SimulateData [DIR] UtilityTools [FILE] Utility_tools.R [DIR] WorkingWithVectors [FILE] dplyr_pgm.R [DIR] dput_dget [FILE] index.html [FILE] sapply_lapply.R
List the contents of the folder¶
In [15]:
import os
folder = r"C:\Explore\Python"
print("Contents of folder:")
for item in sorted(os.listdir(folder)):
print(item)
Contents of folder: .ipynb_checkpoints .virtual_documents BDM_incident_category_optimized_Updated12.py BD_Census_Chart1.py BD_Census_RC_Chart2.py Concatenate_csv_files.py Desscription_concatenate_pgm1.txt Earlier_index.html Extra_Pgms FindCopyRenameHTMLFiles.py Find_index_HTML.py Generate_frequency_table.py Incident_chart1_table.py Incident_chart2_table.py Keywords_list.py Keywords_list_v3_SearchThemes.py NonRecursiverFullPathFileNames.py NonrecursiveResultsSavedInFile.py PrintContensOfTheFolder.py Program2_YT_Search_20260808_updated_revised_v3.py PythonInstallationAuditReport.txt Read_Excel_EMB_2025.py Read_Excel_EMB_Aug31_Dec31_2024.py RecursiveFilesSearch_rglob.py RecursiveFullPathFileNames.py RecusiveNonrecursivePathFiles.py Test_conda.py TreeFormatRecusrive.py UnderstandPythonEnvironment.py Untitled.ipynb WhichPythonInterpreterSpyderUsing.py Write_Output_To_File.py Write_Output_To_File.txt anaconda_projects combine_census_charts.py combine_incident_charts.py concatenate_2025_df_updated2.py conda_check.py index.html subset_df.py
Recursively print the full path of all .txt files¶
In [27]:
# Recursively print the full path of all .txt files
import os
folder = r"C:\Explore\PythonBDM_YouTube"
for root, dirs, files in os.walk(folder):
for filename in sorted(files):
if filename.lower().endswith(".txt"):
print(os.path.join(root, filename))
C:\Explore\PythonBDM_YouTube\Spyder_Python_Console_08032026.txt C:\Explore\PythonBDM_YouTube\Obsolete_YT_Python_Pgms\Python_console_log.txt C:\Explore\PythonBDM_YouTube\Obsolete_YT_Python_Pgms\TopFiveVideos.txt C:\Explore\PythonBDM_YouTube\Original_YT_PGMS\hashtags.txt
Recursively print the full path of a particular file¶
In [59]:
# Recursively print the full path of a particular file.
from pathlib import Path
# Root folder
root = Path(r"C:\Explore")
print(f"Searching under: {root}\n")
# Find all index.html files recursively
for file in root.rglob("index.html"):
print(file)
Searching under: C:\Explore C:\Explore\index.html C:\Explore\DataHive\index.html C:\Explore\HTML\index.html C:\Explore\Python\index.html C:\Explore\PythonBDM_YouTube\index.html C:\Explore\r-basics\index.html C:\Explore\SAS\index.html C:\Explore\stata-complex-surveys\index.html C:\Explore\SUDAAN\index.html C:\Explore\DataHive\BDIndiaCensusHistorical\index.html C:\Explore\DataHive\BDViolenceAgainstMinority\index.html
Non-recursively print the full path of all .R files - top folder only¶
In [67]:
# Non-recursively print the full path of all .R files - top folder only
import os
folder = r"C:\Explore\r-basics"
for filename in sorted(os.listdir(folder)):
if filename.endswith(".R"):
full_path = os.path.join(folder, filename)
if os.path.isfile(full_path):
print(full_path)
C:\Explore\r-basics\DataManipulation.R C:\Explore\r-basics\DatasetsInRPackages.R C:\Explore\r-basics\ReadingWritingDataFiles.R C:\Explore\r-basics\Utility_tools.R C:\Explore\r-basics\dplyr_pgm.R C:\Explore\r-basics\sapply_lapply.R
Recursively print the folder structure and file names in a graphical tree format¶
In [156]:
# --------------------------------------------------------
# Recursively prints the folder structure and file names
# in a graphical tree format
# --------------------------------------------------------
import os
# Root folder (already exists)
root_folder = r"C:\Explore\DataHive"
def print_tree(startpath, prefix=""):
"""
Recursively prints the folder structure in a graphical tree format.
"""
entries = sorted(os.listdir(startpath)) # Sort for consistent order
for i, entry in enumerate(entries):
path = os.path.join(startpath, entry)
connector = "└── " if i == len(entries) - 1 else "├── "
print(prefix + connector + entry)
if os.path.isdir(path):
extension = " " if i == len(entries) - 1 else "│ "
print_tree(path, prefix + extension)
# Display the folder tree
print(f"Folder structure under '{root_folder}':\n")
print_tree(root_folder)
Folder structure under 'C:\Explore\DataHive': ├── BDIndiaCensusHistorical │ ├── Before_index.html │ ├── Charts │ │ ├── Combined_Population_Charts.pdf │ │ ├── Percent_Hindu_BD_Muslim_India.pdf │ │ ├── ReligiousComposition_Count.pdf │ │ └── ReligiousComposition_Percent.pdf │ ├── Obsolete_BD_India_index.html │ └── index.html ├── BDViolenceAgainstMinority │ ├── BDViolenceAM_Charts │ │ ├── Combined_Incident_Charts.pdf │ │ └── Combined_Incident_Charts.png │ ├── BDViolenceAM_Reports │ ├── BDViolenceAM_VideoResources │ │ ├── YouTube_Videos_2024.html │ │ ├── YouTube_Videos_2025.html │ │ └── YouTube_Videos_2026.html │ ├── DataTables │ │ ├── 2023AndEarlier │ │ ├── 2024 │ │ │ └── BD_MVictims_Aug21_Dec31_2024db.html │ │ ├── 2025 │ │ │ └── BD_Minority_Victims_2025_dashboard.html │ │ └── 2026 │ ├── GoogleNotebookLM_BDViolenceAM │ │ ├── 2025_Anatomy_of_Persecution.pdf │ │ ├── 2025_Bangladesh_Forensic_Dossier.pdf │ │ └── 2025_Violence_Dashboard.mp4 │ ├── index.html │ └── original_index.html ├── index.html └── obsolete_index.html
Recursively locate anaconda_projects folders and print their paths¶
In [154]:
from pathlib import Path
root = Path(r"C:\Explore")
folders = [
folder for folder in root.rglob("anaconda_projects")
if folder.is_dir()
]
print(f"Found {len(folders)} folder(s):\n")
for folder in folders:
print(folder)
Found 22 folder(s): C:\Explore\Python\anaconda_projects C:\Explore\PythonBDM_YouTube\anaconda_projects C:\Explore\SAS\anaconda_projects C:\Explore\SAS\Lesson1\anaconda_projects C:\Explore\SAS\Lesson10\anaconda_projects C:\Explore\SAS\Lesson11\anaconda_projects C:\Explore\SAS\Lesson12\anaconda_projects C:\Explore\SAS\Lesson13\anaconda_projects C:\Explore\SAS\Lesson14\anaconda_projects C:\Explore\SAS\Lesson15\anaconda_projects C:\Explore\SAS\Lesson16\anaconda_projects C:\Explore\SAS\Lesson17\anaconda_projects C:\Explore\SAS\Lesson2\anaconda_projects C:\Explore\SAS\Lesson3\anaconda_projects C:\Explore\SAS\Lesson4\anaconda_projects C:\Explore\SAS\Lesson5\anaconda_projects C:\Explore\SAS\Lesson6\anaconda_projects C:\Explore\SAS\Lesson7\anaconda_projects C:\Explore\SAS\Lesson8\anaconda_projects C:\Explore\SAS\Lesson9\anaconda_projects C:\Explore\SAS\Lesson1\SAS_Codes\anaconda_projects C:\Explore\SAS\Lesson2\SAS_Codes\anaconda_projects
In [ ]: