2017-12-09 15 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"で終わる場合は非表示にしたくありません。それがフォルダの場合、あなたが入っているフォルダまたはその親であれば、そのフォルダを隠したくありません。

関連する問題