
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to remove hidden files and folders using Python?
On Unix OS(OSX, Linux, etc) hidden files start with a '.' so we can filter them out using a simple startswith check. On windows, we need to check file attributes and then determine whether the file/folder is hidden or not.
Example
For example, you can use the following code to delete all hidden files:
import os if os.name == 'nt': import win32api, win32con def file_is_hidden(p): if os.name== 'nt': attribute = win32api.GetFileAttributes(p) return attribute & (win32con.FILE_ATTRIBUTE_HIDDEN | win32con.FILE_ATTRIBUTE_SYSTEM) else: return p.startswith('.') #linux-osx [os.remove(f) for f in os.listdir('.') if file_is_hidden(f)]
- Related Articles
- How to get hidden files and folders using PowerShell?
- How to delete hidden files and folders using PowerShell?
- How to display files/folders including hidden files/folders in PowerShell?
- How to get only hidden files and folders in PowerShell?
- How to copy readonly and hidden files/folders in the PowerShell?
- How to retrieve files and folders attributes using PowerShell?
- How to change files and folders attributes using PowerShell?
- How to delete empty files and folders using PowerShell?
- How to ignore hidden files using os.listdir() in Python?
- How to list non-hidden files and directories in windows using Python?
- How to remove swap files using Python?
- How to Zip / Unzip files or folders using PowerShell?
- How to delete all files and folders from a path in C#?
- How to get only files but not the folders with Get-ChildItem using PowerShell?
- Write a C program to print all files and folders.

Advertisements