Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

List Non-Hidden Files and Directories in Windows using Python



Listing files and directories using Python is a common task in many applications, from file management tools to automated scripts. However, when we are working on the Windows Operating System, the hidden files and folders are marked using specific file attributes, which can clutter the output if not handled properly.

Unlike Unix-based systems, where hidden files typically start with a dot such as .hidden) whereas Windows uses metadata to mark files as hidden. In this article, we'll explore several methods to list only non-hidden files and directories.

Basic Filtering by Name Prefix

This is the basic Filtering method, which checks if the filename starts with a dotThis method works on Unix-like systems but not on Windows because hidden files do not use this naming convention in Windows.

Example

Following is an example of listing the non-hidden files and directories in Windows using Python -

import os

files = [f for f in os.listdir('.') if not f.startswith('.')]
print(files)

Following is the output of the above program ?

config.py
empty_file.txt
file1.txt
file2.txt
helper.py

Note: This will not exclude hidden files on Windows that do not start with a dot.

Using os.scandir() with Windows API Check

The most accurate way to detect hidden files on Windows is by checking their system attributes. Python's built-in ctypes module can interface with the Windows API to retrieve file attributes, while os.scandir() provides an efficient way to iterate through directory contents.

Each file or directory on Windows has a set of attributes, including the FILE_ATTRIBUTE_HIDDEN flag. By calling the Windows API function GetFileAttributesW, we can determine whether the hidden attribute is set for a given path.

Example

Following is an example, in which we use the os.scandir() method to get the list of non-hidden files and directories in Windows using Python ?

import os
import ctypes

FILE_ATTRIBUTE_HIDDEN = 0x2

def is_hidden(filepath):
attrs = ctypes.windll.kernel32.GetFileAttributesW(str(filepath))
return attrs != -1 and (attrs & FILE_ATTRIBUTE_HIDDEN)

def list_non_hidden(path):
with os.scandir(path) as entries:
   for entry in entries:
      if not is_hidden(entry.path):
         print(entry.name)

list_non_hidden("D:\Tutorialspoint\Articles")

This method accurately filters non-hidden files using Windows file attributes.

config.py
empty_file.txt
file1.txt
file2.txt
helper.py

Using os.walk() with Attribute Filtering

If we want to recursively walk through the directories and list non-hidden files, then we can combine the method os.walk() with the method is_hidden(). Following is the example which shows how to use the os.walk() method to get the list of non-hidden files and directories in Windows using Python ?

import os
import ctypes

# Constant for the hidden file attribute
FILE_ATTRIBUTE_HIDDEN = 0x2

def is_hidden(filepath):
   """Check if a file or folder is hidden using Windows API."""
   attrs = ctypes.windll.kernel32.GetFileAttributesW(str(filepath))
   return attrs != -1 and (attrs & FILE_ATTRIBUTE_HIDDEN)

def walk_non_hidden(path):
   """Recursively walk through directory tree and list non-hidden files."""
   for root, dirs, files in os.walk(path):
      # Filter hidden directories and files
      dirs[:] = [d for d in dirs if not is_hidden(os.path.join(root, d))]
      files = [f for f in files if not is_hidden(os.path.join(root, f))]
      # Print full path of non-hidden files
      for name in files:
         print(os.path.join(root, name))

# Example usage
walk_non_hidden("D:\Tutorialspoint\Articles")

Below is the output of the above program ?

D:\Tutorialspoint\Articles\config.py
D:\Tutorialspoint\Articles\empty_file.txt
D:\Tutorialspoint\Articles\file1.txt
D:\Tutorialspoint\Articles\file2.txt
D:\Tutorialspoint\Articles\helper.py

Using pathlib

Python's pathlib module is a modern object-oriented interface. However, it doesn't detect hidden files on Windows without using the help of ctypes. Here is an example, which uses the pathlib module to list non-hidden files and directories in Windows using Python ?

from pathlib import Path
import ctypes

def is_hidden(path):
    return ctypes.windll.kernel32.GetFileAttributesW(str(path)) & 0x2

def list_non_hidden(path):
    for item in Path(path).iterdir():
        if not is_hidden(item):
            print(item.name)

list_non_hidden("D:\Tutorialspoint\Articles")

Here is the output of the above program ?

config.py
empty_file.txt
file1.txt
file2.txt
helper.py
Updated on: 2025-05-15T15:26:41+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements