python判断windows隐藏文件的方法

yipeiwu_com6年前Python基础

1. 通过windows attrib 命令获取文件隐藏属性

复制代码 代码如下:
Syntax
      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 官网对 win32API 的简单说明 https://www.python.org/download/windows/
下载地址 http://sourceforge.net/projects/pywin32/

复制代码 代码如下:
import win32file
·
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)


运行结果:

4. 与运算(&)更直观判断隐藏文件
示例代码如下,& 运算的结果与隐藏属性值相对应,可以更直观的判断文件类型。

复制代码 代码如下:
import win32file
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)

运行结果:

 

相关文章

Python中文件的读取和写入操作

从文件中读取数据 读取整个文件 这里假设在当前目录下有一个文件名为'pi_digits.txt'的文本文件,里面的数据如下: 3.1415926535 8979323846 2643...

wxpython布局的实现方法

wxpython布局的实现方法

我们目前已经学会了四个控件,也编出了几个窗口实例,它们都有一个共同的特点,就是丑,主要原因是没有进行合理地布局。 此前的布局方式简单粗暴,即明确规定每个控件的大小和位置,从而使之固定。这...

对python字典元素的添加与修改方法详解

1、字典中的键存在时,可以通过字典名+下标的方式访问字典中改键对应的值,若键不存在则会抛出异常。如果想直接向字典中添加元素可以直接用字典名+下标+值的方式添加字典元素,只写键想后期对键赋...

详解python列表生成式和列表生成式器区别

本文实例为大家分享了python(列表生成式/器)的具体代码,供大家参考,具体内容如下 一、列表生成式 #列表生成式是快速生成一个列表的一些公式 numbers = [] for...

【python】matplotlib动态显示详解

【python】matplotlib动态显示详解

1.matplotlib动态绘图 python在绘图的时候,需要开启 interactive mode。核心代码如下: plt.ion(); #开启interactive mode...