【发布时间】:2022-01-24 04:09:27
【问题描述】:
我正在开发一个加载/保存模块(用 Python 编写的 GUI),该模块将与未来的程序一起使用并导入到未来的程序中。我的操作系统是 Windows 10。我遇到的问题是我的 get_folders() 方法正在获取所有文件夹名称,包括我宁愿忽略的文件夹名称,例如系统文件夹和隐藏文件夹(最好在 c 驱动器上看到)。
我有一个使用硬编码排除列表的方法。但这仅适用于列表中已经存在的文件夹,不适用于我的向导将来可能遇到的隐藏文件夹。我想排除设置了“隐藏”属性的 ALL 文件夹。我想避免需要安装新库的方法,每当我擦除系统时都必须重新安装这些库。此外,如果解决方案不是特定于 Windows 的,但也适用于 Windows 10,那就更好了。
我已经在 SO 和网络上搜索了答案,但没有找到答案。我找到的最接近的答案包含在此线程的答案 #4 中:Check for a folder, then create a hidden folder in Python,它显示了如何在创建新目录时设置隐藏属性,而不是如何读取现有目录的隐藏属性。
这是我的问题:有没有人知道一种方法来检查文件夹的“隐藏”属性是否已设置,使用本机 python、pygame 或 os。命令?或者,如果没有原生答案,我会接受从库中导入的方法来实现我的目标。
以下程序演示了我的 get_folders() 方法,并显示了手头的问题:
# Written for Windows 10
import os
import win32gui
CLS = lambda :os.system('cls||echo -e \\\\033c') # Clear-Command-Console function
def get_folders(path = -1, exclude_list = -1):
if path == -1: path = os.getcwd()
if exclude_list == -1: exclude_list = ["Config.Msi","Documents and Settings","System Volume Information","Recovery","ProgramData"]
dir_list = [entry.name for entry in os.scandir(path) if entry.is_dir()] if exclude_list == [] else\
[entry.name for entry in os.scandir(path) if entry.is_dir() and '$' not in entry.name and entry.name not in exclude_list]
return dir_list
def main():
HWND = win32gui.GetForegroundWindow() # Get Command Console Handle.
win32gui.MoveWindow(HWND,100,50,650,750,False) # Size and Position Command Console.
CLS() # Clear Console Screen.
print(''.join(['\n','Folder Names'.center(50),'\n ',('-'*50).center(50)]))
# Example 1: Current Working Directory
dirs = get_folders() # Get folder names in current directory (uses exclude list.)
for elm in dirs:print(' ',elm) # Show the folder names.
print('','-'*50)
# Examle 2: C Drive, All Folders Included
dirs = get_folders('c:\\', exclude_list = []) # Get a list of folders in the root c: drive, nothing excluded.
for elm in dirs: print(' ',elm) # Show the fiolder names
print('','-'*50)
# Example 3: C Drive, Excluded Folder List Work-Around
dirs = get_folders('c:\\') # Get a list of folders in the root c: drive, excluding sytem dirs those named in the exclude_list.
for elm in dirs:print(' ',elm) # Show the folder names.
print("\n Question: Is there a way to identify folders that have the 'hidden' attribute\n\t set to True, rather than using a hard-coded exclusion list?",end='\n\n' )
# ==========================
if __name__ == "__main__":
main()
input(' Press [Enter] to Quit: ')
CLS()
【问题讨论】:
-
我认为answer 会帮助你
-
亚历山大,这正是我所需要的。答案充满了精彩的技巧和参考资料,在很长一段时间内,它将成为我学习和灵感的源泉。谢谢!
标签: python operating-system windows-10