
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
Remove Hidden Files and Folders Using Python
Managing hidden files and folders in a directory is an essential part of building cleanup scripts or automation tools. On Windows, hidden files are not prefixed with a dot but instead they are marked using specific file attributes. In this article, we will explore how to remove those hidden files and directories programatically using Python.
Filtering by Dot Prefix (Unix/Linux/macOS)
On Unix-based systems, the files or folders starting with a dot "." are considered hidden. We can use this naming pattern to filter and remove hidden files. Here is an example of removing the hidden files and directories present in Unix-based systems -
import os def remove_hidden_unix(path): for filename in os.listdir(path): if filename.startswith('.'): filepath = os.path.join(path, filename) if os.path.isfile(filepath): os.remove(filepath) print(f"Removed: {filepath}") elif os.path.isdir(filepath): os.rmdir(filepath) print(f"Removed directory: {filepath}") remove_hidden_unix("D:\Tutorialspoint\Articles")
Following is the output of the above program ?
remove_hidden_unix("D:\Tutorialspoint\Articles")
Note: This method only works where hidden files follow the dot notation convention.
Using Windows API (ctypes) to Detect Hidden Attributes
In Windows, hidden files are marked using file attributes. We can use the ctypes library to interface with the Windows API and remove hidden items. Here is an example of using the Windows API, i.e., cty[es to detect hidden attributes -
import os import ctypes import shutil FILE_ATTRIBUTE_HIDDEN = 0x2 def is_hidden(filepath): return ctypes.windll.kernel32.GetFileAttributesW(str(filepath)) & FILE_ATTRIBUTE_HIDDEN def remove_hidden_windows(path): for root, dirs, files in os.walk(path, topdown=False): for f in files: file_path = os.path.join(root, f) if is_hidden(file_path): os.remove(file_path) print(f"Removed hidden file: {file_path}") for d in dirs: dir_path = os.path.join(root, d) if is_hidden(dir_path): shutil.rmtree(dir_path) print(f"Removed hidden folder: {dir_path}") remove_hidden_windows("D:\Tutorialspoint\Articles")
Here is the output of the above program -
remove_hidden_unix("D:\Tutorialspoint\Articles")
Cross-Platform Check Using pathlib (with ctypes)
The pathlib module provides a modern object-oriented approach. On Windows, it still needs to use ctypes to check attributes but the syntax is more readable.
from pathlib import Path import ctypes import shutil def is_hidden(path): return ctypes.windll.kernel32.GetFileAttributesW(str(path)) & 0x2 def remove_hidden_with_pathlib(path): for item in Path(path).rglob('*'): if is_hidden(item): if item.is_file(): item.unlink() print(f"Removed hidden file: {item}") elif item.is_dir(): shutil.rmtree(item) print(f"Removed hidden folder: {item}") remove_hidden_with_pathlib("D:\Tutorialspoint\Articles")
Using os.stat and the stat Module (Limited Use)
On Unix systems, file mode bits can be checked using the stat module, though this doesn't help with Windows hidden attributes. However, we can detect and skip dot-prefixed files here too.
import os import stat def remove_dot_hidden_unix(path): for filename in os.listdir(path): if filename.startswith('.'): os.remove(os.path.join(path, filename)) print(f"Removed hidden file: {filename}") remove_dot_hidden_unix("D:\Tutorialspoint\Articles")
Each of the above methods has its platform-specific considerations. For Windows systems, use Windows API checks via ctypes. For Unix-based systems, filtering by dot-prefix is generally sufficient.