How do I hide all excluding a file type

一个人想着一个人 提交于 2019-12-10 21:05:17

问题


I'm trying to hide all my files excluding .exe.

Below hides: files, exe

Does not hide: folders

I want: Hide folders, files

Does not hide: .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)

I cannot use -onefile due to large size for each exe.


回答1:


You almost got it ;)

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)

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.



来源:https://stackoverflow.com/questions/47726633/how-do-i-hide-all-excluding-a-file-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!