Program12

12. Develop a program to display contents of a folder recursively (Directory) having sub-folders and files (name and type).

Objective of the Program

  • To understand directory handling

  • To use os module

  • To apply recursion

  • To differentiate between files and folders

  • To display hierarchical structure


Logical Construction (Think Before You Code)

Suppose we have this folder:

MyFolder/

├── file1.txt

├── file2.py

├── SubFolder1/

│   ├── file3.doc

└── SubFolder2/

    ├── file4.jpg


The program should display:
MyFolder
    file1.txt (File)
    file2.py (File)
    SubFolder1 (Directory)
        file3.doc (File)
    SubFolder2 (Directory)
        file4.jpg (File)

How to Solve?

  1. Read folder path from user

  2. Use os.listdir() to get contents

  3. For each item:

    • If item is file → print as File

    • If item is directory → print as Directory

    • Call function again (recursively)

👉 Key Idea:

If item is directory → call same function again.

This is called Recursion.

Flowchart (Block Diagram of Logic)


Flowchart Explanation

  • Start

  • Read folder path

  • List contents

  • For each item:

    • If file → print

    • If directory → print

      • Call function recursively

  • Stop


Algorithm

  1. Start

  2. Define function display_folder(path)

  3. Get list of contents using os.listdir()

  4. For each item in list:

    • Join path using os.path.join()

    • If item is file → print name and type

    • If item is directory →

      • Print name and type

      • Call display_folder() again

  5. Read folder path

  6. Call function

  7. Stop


Python Program

⚠ Students must complete missing parts.


# Program to display folder contents recursively


import os


def display_folder(path):

    items = os.____________(path)

    

    for item in items:

        full_path = os.path.____________(path, item)

        

        if os.path.____________(full_path):

            print("File:", item)

        

        elif os.path.____________(full_path):

            print("Directory:", item)

            display_folder(____________)


folder_path = input("Enter folder path: ")


if os.path.exists(folder_path):

    display_folder(folder_path)

else:

    print("Invalid folder path")


Probable Output

Enter folder path: C:\MyFolder

File: file1.txt
File: file2.py
Directory: SubFolder1
File: file3.doc
Directory: SubFolder2
File: file4.jpg

Important Concepts Used

  • os module

  • os.listdir()

  • os.path.join()

  • os.path.isfile()

  • os.path.isdir()

  • Recursion

  • Path validation


Viva Voce Questions

  1. What is recursion?

  2. Why do we use os.path.join()?

  3. Difference between file and directory?

  4. What is base condition in recursion?

  5. What happens if folder contains many nested folders?

  6. What is difference between listdir() and walk()?

  7. Can we do this without recursion?


Procedure to Execute the Program

  1. Create some folders and subfolders

  2. Add sample files

  3. Run the program

  4. Enter folder path

  5. Observe output


Google Colab – Online Execution

👉 Click the link below to run the complete working version of this program online:
[https://colab.research.google.com/drive/1vr21YSqZRTfZ0Ie2HNalA4oLkqL8Hl05?usp=sharing]


Program Explanation





Assignment for Students

  • Display file size

  • Count total number of files

  • Count total number of directories

  • Implement using os.walk()

  • Display file extension separately

Comments

Popular posts from this blog