File ẩn: Unix/Linux/macOS — tên bắt đầu bằng .; Windows — attribute FILE_ATTRIBUTE_HIDDEN hoặc SYSTEM.
Hàm kiểm tra cross-platform
import os
def is_hidden(path):
if os.name == 'nt': # Windows
import win32api, win32con
attrs = win32api.GetFileAttributes(path)
return bool(attrs & (win32con.FILE_ATTRIBUTE_HIDDEN | win32con.FILE_ATTRIBUTE_SYSTEM))
else: # Unix/Linux/macOS
return os.path.basename(path).startswith('.')
# Xóa file ẩn trong thư mục hiện tại
for f in os.listdir('.'):
if is_hidden(f):
os.remove(f)
print(f"Deleted hidden: {f}")
Yêu cầu Windows
pip install pywin32
Xóa thư mục ẩn (đệ quy)
import shutil
for item in os.listdir('.'):
if is_hidden(item):
if os.path.isdir(item):
shutil.rmtree(item)
else:
os.remove(item)