Heim >Backend-Entwicklung >Python-Tutorial >python判断windows隐藏文件的方法
1. 通过windows attrib 命令获取文件隐藏属性
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
for filename in filenames:
print '%4d, %s' %(win32file.GetFileAttributesW(filename), filename)
4. 与运算(&)更直观判断隐藏文件
示例代码如下,& 运算的结果与隐藏属性值相对应,可以更直观的判断文件类型。
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)
运行结果: