
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to ignore hidden files using os.listdir() in Python?
When we are working with directories in Python, a clutter of hidden files can be created. Hidden files are created with a dot (.) on Unix-like operating systems and we can filter them easily.
In this article, we will explore different methods to ignore hidden files using the os module and pathlib.
Using List Comprehension with os.listdir()
In Python, when we use the os.listdir() method the hidden files i.e., the files those are starting with a dot . are also included in the result. To ignore those hidden files and list only visible files we need to pass a condition within a list comprehension along with the os.listdir() method.
Example
In the following example, we are using the os.listdir() method and list comprehension together to ignore the hidden files -
import os # List all non-hidden files visible_files = [file for file in os.listdir('.') if not file.startswith('.')] print(visible_files)
Following is the output of the above program -
['Archive.zip', 'backup.tar', 'basic-calculator-program-using-python-program.htm', 'config.py', 'empty_file.txt', 'file1.txt', 'file2.txt', 'helper.py']
Using a For Loop to Ignore Hidden Files
We can use a for loop instead of using list comprehension which loops through all the files returned by os.listdir() and manually filter the hidden files which are starting with a dot ..
Example
Following is the example, in which we use a for loop to print only the visible files from the current directory and ignore the hidden files -
import os # Print only non-hidden files for file in os.listdir('.'): if not file.startswith('.'): print(file)
Below is the output of the above program -
Archive.zip backup.tar basic-calculator-program-using-python-program.htm config.py empty_file.txt file1.txt file2.txt helper.py
Using Helper Function for Hidden Files
When working with file filtering in Python, creating a helper function makes our code to reuse. To ignore the hidden files in the given directory, we have to define a custom function that checks whether a file name starts with a dot (.) and filter files returned by os.listdir() method.
Example
Following is the example, in which we define a helper function to exclude hidden files and apply it using a for -
import os # Helper function to check for hidden files def is_visible(file_name): return not file_name.startswith('.') # Use helper function to print visible files for file in os.listdir('.'): if is_visible(file): print(file)
Here is the output of the above program -
Archive.zip backup.tar basic-calculator-program-using-python-program.htm config.py empty_file.txt file1.txt file2.txt helper.py
Using pathlib.Path.iterdir() to Ignore Hidden Files
In Python, pathlib is an object-oriented module to handle file system paths. This module have Path.iterdir() method to iterate over items in a directory and filter the files that start with a dot (.).
Example
Here is the example of ignoring the hidden files in the defined directory by using the path.iterdir() method of the pathlib module -
from pathlib import Path # Get current directory path current_dir = Path('.') # Print only non-hidden files for file in current_dir.iterdir(): if not file.name.startswith('.'): print(file.name)
Following is the output of the above program -
Archive.zip backup.tar basic-calculator-program-using-python-program.htm config.py empty_file.txt file1.txt file2.txt helper.py