python判斷windows隱藏文件的方法
1. 通過(guò)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 通過(guò) win32api 獲取文件隱藏屬性
python 官網(wǎng)對(duì) win32API 的簡(jiǎn)單說(shuō)明 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é)果與隱藏屬性值相對(duì)應(yīng),可以更直觀的判斷文件類(lèi)型。
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多版本開(kāi)發(fā)環(huán)境管理工具介紹
這篇文章主要介紹了Python多版本開(kāi)發(fā)環(huán)境管理工具介紹的相關(guān)資料,在Python開(kāi)發(fā)中,有些情況下,我們可能面臨在一臺(tái)機(jī)器上同時(shí)安裝多版本Python的需求,需要的朋友可以參考下2019-07-07
不可錯(cuò)過(guò)的十本Python好書(shū)
不可錯(cuò)過(guò)的十本Python好書(shū),分別適合入門(mén)、進(jìn)階到精深三個(gè)不同階段的人來(lái)閱讀,感興趣的小伙伴們可以參考一下2017-07-07
python光學(xué)仿真實(shí)現(xiàn)光線追跡之空間關(guān)系
這篇文章主要介紹了python光學(xué)仿真中實(shí)現(xiàn)光線追跡的空間關(guān)系示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10
Python對(duì)Tornado請(qǐng)求與響應(yīng)的數(shù)據(jù)處理
這篇文章主要介紹了Python對(duì)Tornado請(qǐng)求與響應(yīng)的數(shù)據(jù)處理,需要的朋友可以參考下2020-02-02
Python機(jī)器學(xué)習(xí)庫(kù)scikit-learn安裝與基本使用教程
這篇文章主要介紹了Python機(jī)器學(xué)習(xí)庫(kù)scikit-learn安裝與基本使用,較為詳細(xì)的介紹了機(jī)器學(xué)習(xí)庫(kù)scikit-learn的功能、原理、基本安裝與簡(jiǎn)單使用方法,需要的朋友可以參考下2018-06-06
python同時(shí)遍歷數(shù)組的索引和值的實(shí)例
今天小編就為大家分享一篇python同時(shí)遍歷數(shù)組的索引和值的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
Python中最快的循環(huán)姿勢(shì)實(shí)例詳解
python給我們提供了多個(gè)循環(huán)方法,比如while循環(huán)、for循環(huán)等,下面這篇文章主要給大家介紹了關(guān)于Python中最快的循環(huán)姿勢(shì),需要的朋友可以參考下2021-11-11

