
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 list non-hidden files and directories in windows 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 is hidden or not.
Example
For example, you can use the following code to get a listing without 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 file_list = [f for f in os.listdir('.') if not file_is_hidden(f)] print(file_list)
- Related Questions & Answers
- Generate temporary files and directories using Python
- How to remove hidden files and folders using Python?
- How to ignore hidden files using os.listdir() in Python?
- Listing out directories and files in Python?
- Listing out directories and files using C#
- How to get hidden files and folders using PowerShell?
- How to delete hidden files and folders using PowerShell?
- How to list out the hidden files in a Directory using Java program?
- C Program to list all files and sub-directories in a directory
- How to list the hidden files in a directory in Java?
- How to Protect Files and Directories from Deleting in Linux
- How to get only hidden files and folders in PowerShell?
- How to remove files and directories in the Linux operating system using the terminal?\n
- How to display files/folders including hidden files/folders in PowerShell?
- How to move a file, group of files, and directories in Linux?
Advertisements