Windows में Non-Hidden Files List करना
Unix OS (OSX, Linux, आदि) पर Hidden Files '.' से Start होती हैं, इसलिए हम Simple startswith Check का उपयोग करके उन्हें Filter Out कर सकते हैं। Windows पर, हमें File Attributes Check करने की आवश्यकता होती है और फिर Determine करना होता है कि File Hidden है या नहीं।
उदाहरण: Hidden Files के बिना Listing प्राप्त करना
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('.')
file_list = [f for f in os.listdir('.') if not file_is_hidden(f)]
print(file_list)
नोट: Windows के लिए pywin32 Package Install करना आवश्यक है: pip install pywin32
आधुनिक Python (3.4+) में Pathlib का उपयोग:
from pathlib import Path
import os
def is_hidden(path):
if os.name == 'nt':
import win32api, win32con
attrs = win32api.GetFileAttributes(str(path))
return bool(attrs & (win32con.FILE_ATTRIBUTE_HIDDEN | win32con.FILE_ATTRIBUTE_SYSTEM))
else:
return path.name.startswith('.')
visible_files = [f.name for f in Path('.').iterdir() if not is_hidden(f)]
print(visible_files)