如何隐藏除文件类型之外的所有内容
How do I hide all excluding a file type
我试图隐藏除 .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)
我无法使用 -onefile,因为每个 exe 的大小都很大。
你差不多明白了 ;)
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
的文档,它也可以用于文件夹。其中留下一些"filtering"。如果您的项目是一个文件,如果它以“.exe”或“.EXE”结尾,您不想隐藏它。如果它是一个文件夹,如果它是您所在的文件夹或其父文件夹,则您不想隐藏它。
我试图隐藏除 .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)
我无法使用 -onefile,因为每个 exe 的大小都很大。
你差不多明白了 ;)
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
的文档,它也可以用于文件夹。其中留下一些"filtering"。如果您的项目是一个文件,如果它以“.exe”或“.EXE”结尾,您不想隐藏它。如果它是一个文件夹,如果它是您所在的文件夹或其父文件夹,则您不想隐藏它。