python - 如何隐藏除文件类型之外的所有内容

标签 python file operating-system hide glob

我试图隐藏我的所有文件,不包括 .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/

相关文章:

linux - 如何让程序可以由具有所有者权限的特定用户在linux上运行

fork() 的输出困惑

algorithm - 死锁检测算法(伪代码)

python - 从实时 Google App Engine 应用程序导出数据的最简单方法是什么?

python - 为什么这个指令不起作用?

c - 读取文件并保存在数组中

python - 如何使用 python 获取文件的扩展 MacOS 属性?

python - 是否有用于重写 pandas 列值的语法糖?

python - 使用应用程序工厂模式将应用程序上下文传递给自定义转换器

FileHandler.level 不覆盖全局级别