How to list non-hidden files and directories in windows using Python?


When coming across and dealing with file and directory operations within the Windows environment using Python, it's not uncommon to come across the hidden files and directories. These hidden files and folders usually are system files and are purposefully kept out of sight from users. Among these, you might uncover configuration files, those fleeting temporary files, and an array of system−related data. However, in most cases, you may need to list only the non−hidden files and directories to avoid disarray and focus on relevant data.

Prepare yourself for a journey through this all−encompassing article. What lies ahead is an exploration of diverse techniques, each geared toward the task of cataloging non−hidden files and directories. Rest assured, our guidance comes packed with step−by−step elucidations and snippets of code that illuminate your path. Whether you prefer the "os" module's pragmatic charm, the utility of the "pathlib" module, or the inventive prowess of third−party libraries, we've got you covered. By the time you're through, you'll be adept at orchestrating the seamless extraction of those unhidden files within directories.

Let's get started on this journey of file handling with Python and learn how to list non−hidden files and directories in Windows!

Using os.listdir() to List Non−hidden Files and Directories

The "os" module in Python provides the "os.listdir()" function, which allows us to obtain a list of items (files and directories) within a given directory. We can use this function along with "os.path" functions to filter out hidden items and list only non−hidden files and directories.

Example

  • In the code below, we import the "os" module, which provides functions for interacting with the operating system, including file and directory operations.

  • The "list_non_hidden_files_and_dirs_with_os_listdir()" function takes the "directory" as input and returns two lists: one containing non−hidden files and the other containing non−hidden directories.

  • We use "os.listdir(directory)" to obtain a list of items (files and directories) within the specified directory.

  • We use list comprehensions to filter out items that start with a dot (indicating hidden items) from the original list, obtaining the "non_hidden_items" list.

  • We then use list comprehensions again to further filter "non_hidden_items" into separate lists for non−hidden files ("non_hidden_files") and non−hidden directories ("non_hidden_dirs").

  • To check if an item is a file or a directory, we use "os.path.isfile(os.path.join(directory, item))" and "os.path.isdir(os.path.join(directory, item))" respectively.

import os

def list_non_hidden_files_and_dirs_with_os_listdir(directory):
    non_hidden_items = [item for item in os.listdir(directory) if not item.startswith('.')]
    non_hidden_files = [item for item in non_hidden_items if os.path.isfile(os.path.join(directory, item))]
    non_hidden_dirs = [item for item in non_hidden_items if os.path.isdir(os.path.join(directory, item))]
    return non_hidden_files, non_hidden_dirs

Using pathlib.Path.iterdir() to List Non−hidden Files and Directories

The "pathlib" module provides a more modern and object−oriented way to handle file paths. We can utilize "Path.iterdir()" to list non−hidden files and directories efficiently.

Example

  • In this example, we import the "Path" class from the "pathlib" module, which represents a file system path.

  • The "list_non_hidden_files_and_dirs_with_pathlib_iterdir()" function takes the "directory" as input and returns two lists: one containing non−hidden files and the other containing non−hidden directories.

  • We create a "Path" object with "Path(directory)" to represent the input directory.

  • We use list comprehensions to filter out items that start with a dot (indicating hidden items) from the "path_object.iterdir()" iterator, obtaining the "non_hidden_items" list.

  • We then use list comprehensions again to further filter "non_hidden_items" into separate lists for non−hidden files ("non_hidden_files") and non−hidden directories ("non_hidden_dirs").

  • To check if an item is a file or a directory, we use "item.is_file()" and "item.is_dir()" respectively.

from pathlib import Path

def list_non_hidden_files_and_dirs_with_pathlib_iterdir(directory):
    path_object = Path(directory)
    non_hidden_items = [item for item in path_object.iterdir() if not item.name.startswith('.')]
    non_hidden_files = [item for item in non_hidden_items if item.is_file()]
    non_hidden_dirs = [item for item in non_hidden_items if item.is_dir()]
    return non_hidden_files, non_hidden_dirs

Using os.scandir() to List Non−hidden Files and Directories

The "os.scandir()" function is a more efficient alternative to "os.listdir()" for obtaining directory contents. We can utilize "os.scandir()" along with "os.path" functions to list non−hidden files and directories.

Example

  • In this example, we use the "os.scandir()" function along with "os.path" functions to list non−hidden files and directories.

  • The "list_non_hidden_files_and_dirs_with_os_scandir()" function takes the "directory" as input and returns two lists: one containing non−hidden files and the other containing non−hidden directories.

  • We use "with os.scandir(directory) as entries" to obtain an iterator of entries (files and directories) within the specified directory.

  • We use list comprehension to filter out entries that start with a dot (indicating hidden items) from the "entries" iterator, obtaining the "non_hidden_items" list.

  • We then use list comprehensions again to further filter "non_hidden_items" into separate lists for non−hidden files ("non_hidden_files") and non−hidden directories ("non_hidden_dirs").

  • To check if an item is a file or a directory, we use "os.path.isfile(os.path.join(directory, item))" and "os.path.isdir(os.path.join(directory, item))" respectively.

import os

def list_non_hidden_files_and_dirs_with_os_scandir(directory):
    with os.scandir(directory) as entries:
        non_hidden_items = [entry.name for entry in entries if not entry.name.startswith('.')]
    non_hidden_files = [item for item in non_hidden_items if os.path.isfile(os.path.join(directory, item))]
    non_hidden_dirs = [item for item in non_hidden_items if os.path.isdir(os.path.join(directory, item))]
    return non_hidden_files, non_hidden_dirs

Using pathlib.Path.glob() to List Non−hidden Files and Directories

The "pathlib" module's "Path.glob()" method allows us to perform pattern matching on file names within a directory. We can use "Path.glob()" to list non−hidden files and directories in a concise manner.

Example

  • In this example, we import the "Path" class from the "pathlib" module and use "Path.glob()" to list non−hidden files and directories.

  • The "list_non_hidden_files_and_dirs_with_pathlib_glob()" function takes the "directory" as input and returns two lists: one containing non−hidden files and the other containing non−hidden directories.

  • We create a "Path" object with "Path(directory)" to represent the input directory.

  • We use list comprehensions to filter out items that start with a dot (indicating hidden items) from the "path_object.glob('*')" generator, obtaining the "non_hidden_items" list.

  • We then use list comprehensions again to further filter "non_hidden_items" into separate lists for non−hidden files ("non_hidden_files") and non−hidden directories ("non_hidden_dirs").

  • To check if an item is a file or a directory, we use "item.is_file()" and "item.is_dir()" respectively.

from pathlib import Path

def list_non_hidden_files_and_dirs_with_pathlib_glob(directory):
    path_object = Path(directory)
    non_hidden_items = [item for item in path_object.glob('*') if not item.name.startswith('.')]
    non_hidden_files = [item for item in non_hidden_items if item.is_file()]
    non_hidden_dirs = [item for item in non_hidden_items if item.is_dir()]
    return non_hidden_files, non_hidden_dirs

Using os.walk() to List Non−hidden Files and Directories

The "os.walk()" function allows us to traverse a directory and its subdirectories. We can use "os.walk()" in conjunction with "os.path" functions to list non−hidden files and directories recursively.

Example

  • In this example, we use "os.walk()" in combination with "os.path" functions to list non−hidden files and directories.

  • The "list_non_hidden_files_and_dirs_with_os_walk()" function takes the "directory" as input and returns two lists: one containing non−hidden files and the other containing non−hidden directories.

  • We initialize empty lists for "non_hidden_files" and "non_hidden_dirs" to store the non−hidden items.

  • We use "os.walk(directory)" to traverse the directory and its subdirectories. For each iteration, we obtain the "root" directory, a list of "dirs" (directories), and a list of "files" within the current directory.

  • We use list comprehension to filter out files and directories that start with a dot (indicating hidden items), and then extend the respective "non_hidden_files" and "non_hidden_dirs" lists.

import os

def list_non_hidden_files_and_dirs_with_os_walk(directory):
    non_hidden_files = []
    non_hidden_dirs = []
    for root, dirs, files in os.walk(directory):
        non_hidden_files.extend([os.path.join(root, f) for f in files if not f.startswith('.')])
        non_hidden_dirs.extend([os.path.join(root, d) for d in dirs if not d.startswith('.')])
    return non_hidden_files, non_hidden_dirs

In conclusion, handling files and directories in Windows using Python can be made more efficient and focused by listing only the non−hidden entities. By using various techniques such as "os.listdir()", "pathlib.Path.iterdir()", "os.scandir()", "pathlib.Path.glob()", and "os.walk()", you can easily filter out hidden files and directories and retrieve the relevant information you need.

Throughout this article, we explored a few different methods to list non−hidden files and directories in Windows. We discussed each method in detail, providing step−by−step explanations and code examples to facilitate your understanding. Whether you prefer the simplicity of "os.listdir()" or the modern approach of "pathlib", you now have a range of techniques at your disposal.

You must always remember to consider your specific use case and choose the method that best suits your requirements. Whether you are dealing with a single directory or a complex directory structure with subdirectories, Python offers versatile tools to efficiently list non−hidden files and directories.

Updated on: 11-Sep-2023

602 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements