Listing Files and Directories

Write a Python program that uses the os module to list all files and directories in the current working directory. Return the list.


This will depend on the files and directories present in your current working directory.


The os module in Python provides functions for interacting with the operating system. os.getcwd() is a method in python to get the current working directory. os.listdir() is used to get the list of all files and directories in the specified directory.

import os

def list_files_directories():
    # Get current working directory
    cwd = os.getcwd()
    # List all files and directories
    return os.listdir(cwd)


© Let’s Data Science


Unlock AI & Data Science treasures. Log in!