Ignore Hidden Files Using os.listdir in Python



When working with directories in Python, hidden files can often create clutter. Hidden files typically start with a dot (.) on Unix-like operating systems, and you may want to filter them out to ensure cleaner handling of your files.

Below are four effective methods to ignore hidden files using the os module and pathlib. Each method includes samples, explanations, and expected outputs.

Using List Comprehension with 'os.listdir()'

One of the simplest and most efficient ways to ignore hidden files when listing a directory's contents is to use list comprehension. List comprehension allows us to filter out specific items based on a condition, and in this case, we can use it to exclude hidden files from the list of files returned by os.listdir().

Example

In the following example, we used the 'os' module for file operations. "list_visible_files_with_list_comprehension()" returns visible files in a directory, filtering out hidden files (those starting with a dot) using list comprehension on the os.listdir() output. The resulting list of visible filenames is then returned.

import os
def list_visible_files_with_list_comprehension(directory):
    visible_files = [file for file in os.listdir(directory) if not file.startswith('.')]
    return visible_files

Using a For Loop to Ignore Hidden Files

An alternative approach to ignoring hidden files when listing a directory's contents is by using a for loop to iterate over the files and exclude the hidden ones from the final list.

Example

The following code iterates through files in a directory using os.listdir(). It initializes an empty list to store visible file names. A for loop checks each item; if the item's name doesn't start with a dot (.), it's appended to the list. Finally, the list of visible files is returned.

import os
def list_visible_files_with_for_loop(directory):
    visible_files = []
    for file in os.listdir(directory):
        if not file.startswith('.'):
            visible_files.append(file)
    return visible_files

Using Helper Function for Hidden Files

To improve code readability and reusability, we can define a helper function that checks whether a given file is hidden or not. We can then use this function to filter out hidden files when listing a directory's contents.

Example

In the following example, 'is_hidden()' function checks if a file starts with a dot, indicating a hidden file. list_visible_files_with_helper_function() returns a list of visible files from a directory. It uses os.listdir() to get all items and filters them using is_hidden(), adding non-hidden files to the visible_files list, which is then returned.

import os

def is_hidden(file):
    return file.startswith('.')

def list_visible_files_with_helper_function(directory):
    visible_files = [file for file in os.listdir(directory) if not is_hidden(file)]
    return visible_files

Using pathlib.Path.iterdir() to Ignore Hidden Files

The pathlib module provides a more modern and object?oriented way to handle file paths. We can utilize "Path.iterdir()" to ignore hidden files and list only visible files from a directory.

Example

This code uses pathlib's "Path" to list visible files in a directory. The function 'list_visible_files_with_pathlib_iterdir()' takes a directory path, creates a Path object, and uses list comprehension with iterdir() to filter out hidden files (names starting with a dot). It returns a list of visible filenames.

from pathlib import Path
def list_visible_files_with_pathlib_iterdir(directory):
    path_object = Path(directory)
    visible_files = [item.name for item in path_object.iterdir() if not item.name.startswith('.')]
    return visible_files
Updated on: 2025-03-17T17:01:40+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements