我试图隐藏我的所有文件,不包括 .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)
由于每个 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)
查看SetFileAttributesW
的文档,它也可以用于文件夹。其中留下一些“过滤”。如果您的项目是一个文件,如果它以“.exe”或“.EXE”结尾,您不想隐藏它。如果它是一个文件夹,如果它是您所在的文件夹或其父文件夹,则您不想隐藏它。
关于python - 如何隐藏除文件类型之外的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47726633/