2017-12-09 4 views
3

.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을 사용할 수 없습니다. 당신은 거의 그것을 가지고

+1

이것은 매우 혼란 스럽습니다. 이 문제를 발생한 문제로 제한하고 다른 모든 것을 제거 할 수 있습니까? –

답변

5

, SetFileAttributesW의 문서를 보면)

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) 

, 그것은뿐만 아니라 폴더에 사용할 수 있습니다. 어떤 "필터링"을 남깁니다. 항목이 파일 인 경우 ".exe"또는 ".EXE"로 끝나는 항목은 숨기고 싶지 않습니다. 그것이 폴더라면, 자신이 속한 폴더이거나 부모 인 경우에는 숨기고 싶지 않습니다.

관련 문제