3 python operating-system glob file hide
我试图隐藏除.exe之外的所有文件.
下面隐藏:文件,exe
不隐藏:文件夹
我想:隐藏文件夹,文件
不隐藏:.exe
import os, shutil
import ctypes
folder = 'C:\\Users\\TestingAZ1'
for the_file in os.listdir(folder):
file_path = os.path.join(folder, the_file)
try:
if os.path.isfile(file_path):
ctypes.windll.kernel32.SetFileAttributesW(file_path, 2)
except Exception as e:
print(e)
Run Code Online (Sandbox Code Playgroud)
由于每个exe的大小,我不能使用-onefile.
你几乎得到了它;)
import os
import ctypes
folder = 'C:\\Users\\TestingAZ1'
for item_name in os.listdir(folder):
item_path = os.path.join(folder, item_name)
try:
if os.path.isfile(item_path) and not item_name.lower().endswith('.exe'):
ctypes.windll.kernel32.SetFileAttributesW(item_path, 2)
elif os.path.isdir(item_path) and item_name not in ['.', '..']:
ctypes.windll.kernel32.SetFileAttributesW(item_path, 2)
except Exception as e:
print(e)
Run Code Online (Sandbox Code Playgroud)
查看文档SetFileAttributesW,它也可以用于文件夹.留下一些"过滤".如果您的项目是文件,如果它以".exe"或".EXE"结尾,则您不想隐藏它.如果它是一个文件夹,如果它是您所在的文件夹或其父文件夹,则不想隐藏它.