如何隐藏所有不包括文件类型的文件 [英] How do I hide all excluding a file type

查看:73
本文介绍了如何隐藏所有不包括文件类型的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试隐藏除 .exe 之外的所有文件..

I'm trying to hide all my files excluding .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.

I cannot use -onefile due to large size for each 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的文档,它也可以用于文件夹.其中留下一些过滤".如果您的项目是文件,则如果它以".exe"或".EXE"结尾,则不想隐藏它.如果它是文件夹,则如果它是您所在的文件夹或其父文件夹,则不想隐藏它.

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.

这篇关于如何隐藏所有不包括文件类型的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆