Both approaches are really effective. Personally, I prefer a method where directories and files are handled separately for more control over the display format. This is particularly useful if you want to apply unique formatting or even add metadata later on.
import os
def print_dir_structure(path):
for root, dirs, files in os.walk(path):
if root == path:
print("A") # Root directory
else:
print(f"---{os.path.basename(root)}") # Subdirectories
for file in files:
print(f"---{file}")
# Call the function for the current directory
print_dir_structure(".")
This solution provides more flexibility when working with the python walk directory structure, especially if your project requires handling directories and files separately.