python判斷windows隱藏文件的方法
1. 通過windows attrib 命令獲取文件隱藏屬性
ATTRIB [ + attribute | - attribute ] [pathname] [/S [/D]]
Key
+ : Turn an attribute ON
- : Clear an attribute OFF
pathname : Drive and/or filename e.g. C:\*.txt
/S : Search the pathname including all subfolders.
/D : Process folders as well
attributes:
R Read-only (1)
H Hidden (2)
A Archive (32)
S System (4)
extended attributes:
E Encrypted
C Compressed (128:read-only)
I Not content-indexed
L Symbolic link/Junction (64:read-only)
N Normal (0: cannot be used for file selection)
O Offline
P Sparse file
T Temporary

2. 隱藏屬性值及其含義
Constants - the following attribute values are returned by the GetFileAttributes function:
FILE_ATTRIBUTE_READONLY = 1 (0x1)
FILE_ATTRIBUTE_HIDDEN = 2 (0x2)
FILE_ATTRIBUTE_SYSTEM = 4 (0x4)
FILE_ATTRIBUTE_DIRECTORY = 16 (0x10)
FILE_ATTRIBUTE_ARCHIVE = 32 (0x20)
FILE_ATTRIBUTE_NORMAL = 128 (0x80)
FILE_ATTRIBUTE_TEMPORARY = 256 (0x100)
FILE_ATTRIBUTE_SPARSE_FILE = 512 (0x200)
FILE_ATTRIBUTE_REPARSE_POINT = 1024 (0x400)
FILE_ATTRIBUTE_COMPRESSED = 2048 (0x800)
FILE_ATTRIBUTE_OFFLINE = 4096 (0x1000)
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 8192 (0x2000)
FILE_ATTRIBUTE_ENCRYPTED = 16384 (0x4000)
For example, a file attribute of 0x120 indicates the Temporary + Archive attributes are set (0x100 + 0x20 = 0x120.)
3. python 通過 win32api 獲取文件隱藏屬性
python 官網(wǎng)對 win32API 的簡單說明 https://www.python.org/download/windows/
下載地址 http://sourceforge.net/projects/pywin32/
·
filenames = [r'D:\test',
r'D:\test\$RECYCLE.BIN',
r'D:\test\.file_test.py.swp',
r'D:\test\file_test.py']
for filename in filenames:
print '%4d, %s' %(win32file.GetFileAttributesW(filename), filename)
運(yùn)行結(jié)果:
4. 與運(yùn)算(&)更直觀判斷隱藏文件
示例代碼如下,& 運(yùn)算的結(jié)果與隱藏屬性值相對應(yīng),可以更直觀的判斷文件類型。
import win32con
filenames = [r'D:\test',
r'D:\test\$RECYCLE.BIN',
r'D:\test\.file_test.py.swp',
r'D:\test\file_test.py']
for filename in filenames:
file_flag = win32file.GetFileAttributesW(filename)
is_hiden = file_flag & win32con.FILE_ATTRIBUTE_HIDDEN
is_system = file_flag & win32con.FILE_ATTRIBUTE_SYSTEM
print '%4d, %s, %s, %s' %(file_flag, is_hiden, is_system, filename)
運(yùn)行結(jié)果:
相關(guān)文章
python使用OS模塊操作系統(tǒng)接口及常用功能詳解
os是?Python?標(biāo)準(zhǔn)庫中的一個(gè)模塊,提供了與操作系統(tǒng)交互的功能,在本節(jié)中,我們將介紹os模塊的一些常用功能,并通過實(shí)例代碼詳細(xì)講解每個(gè)知識(shí)點(diǎn)2023-06-06Django中日期時(shí)間型字段進(jìn)行年月日時(shí)分秒分組統(tǒng)計(jì)
這篇文章主要介紹了Django中日期時(shí)間型字段進(jìn)行年月日時(shí)分秒分組統(tǒng)計(jì),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11使用jupyter notebook輸出顯示不完全的問題及解決
這篇文章主要介紹了使用jupyter notebook輸出顯示不完全的問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02文件上傳服務(wù)器-jupyter 中python解壓及壓縮方式
這篇文章主要介紹了文件上傳服務(wù)器-jupyter 中python解壓及壓縮方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04