发布于 2014-08-21 15:30:35 | 256 次阅读 | 评论: 0 | 来源: 网友投递
Python编程语言
Python 是一种面向对象、解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年。Python语法简洁而清晰,具有丰富和强大的类库。它常被昵称为胶水语言,它能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结在一起。
本文讲解了python如何判断windows下的隐藏方件方法,linux 下隐藏文件是以句号 “.” 开头的文件,根据文件名即可判断是否为隐藏文件。win 下是以文件隐藏属性确定的,所以,只能通过微软的 API 获取隐藏属性来判断是否为隐藏文件,感兴趣的同学参考下.
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
2. 隐藏属性值及其含义
Constants - the following attribute values are returned by the GetFileAttributes function:
For example, a file attribute of 0x120 indicates the Temporary + Archive attributes are set (0x100 + 0x20 = 0x120.)
3. python 通过 win32api 获取文件隐藏属性
python 官网对 win32API 的简单说明 https://www.python.org/download/windows/
下载地址 http://sourceforge.net/projects/pywin32/
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:testfile_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)
运行结果: