How Do I Hide All Excluding A File Type
I'm trying to hide all my files excluding .exe. Below hides: files, exe Does not hide: folders I want: Hide folders, files Does not hide: .exe import os, shutil import ctypes folde
Solution 1:
You almost got it ;)
import os
import ctypes
folder = 'C:\\Users\\TestingAZ1'for item_name inos.listdir(folder):
item_path = os.path.join(folder, item_name)
try:
ifos.path.isfile(item_path) andnot item_name.lower().endswith('.exe'):
ctypes.windll.kernel32.SetFileAttributesW(item_path, 2)
elif os.path.isdir(item_path) and item_name notin ['.', '..']:
ctypes.windll.kernel32.SetFileAttributesW(item_path, 2)
except Exception as e:
print(e)
Looking at the documentation of SetFileAttributesW
, it can be used for folders as well. Which leave some "filtering". If your item is a file, you do not want to hide it if it ends on ".exe" or ".EXE". If it's a folder, you do not want to hide it if it is the folder you're in or its parent.
Post a Comment for "How Do I Hide All Excluding A File Type"