How do I list all files of a directory?

22    Asked by KalyaniMalhotra in Python , Asked on Mar 27, 2025

Looking to list all the files in a directory? This guide shows you simple ways to do it in various programming languages, including Python, Java, and Linux command line!

Answered by RichardBrown

If you want to list all the files in a directory, the method depends on the environment or programming language you're using. Here are some easy ways to get the job done:

1. Using Command Line (Linux/Mac/Windows)

Linux/Mac Terminal:

 Type the following command to list files in the current directory:

  ls

 For more details (like file permissions), use:

  ls -l

Windows Command Prompt:

 Run:

  dir

2. Using Python

Python provides simple functions in the os and os.path modules to list files:

import os  
files = os.listdir("path/to/directory")

for file in files:

    print(file)

3. Using Java

In Java, you can list files using the File class:

import java.io.File;  
public class ListFiles {
    public static void main(String[] args) {
        File folder = new File("path/to/directory");
        for (File file : folder.listFiles()) {
            System.out.println(file.getName());
        }
    }
}

These methods will help you list all files in a directory based on your preferred platform. If you need recursive listing (files inside subdirectories), additional tweaks may be needed, like using os.walk() in Python!



Your Answer

Interviews

Parent Categories