Python methods for judging windows hidden files

Source: Internet
Author: User
1. Get file-hidden properties from the Windows attrib command
Copy CodeThe code is as follows:

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 is used for file selection)
O Offline
P Sparse File
T Temporary




2. Hide attribute values and their meanings
Constants-the following attribute values is returned by the GetFileAttributes function:

Copy the Code code as follows:


File_attribute_readonly = 1 (0x1)
File_attribute_hidden = 2 (0x2)
File_attribute_system = 4 (0x4)
File_attribute_directory = (0x10)
File_attribute_archive = + (0x20)
File_attribute_normal = (0x80)
File_attribute_temporary = (0x100)
File_attribute_sparse_file = (0x200)
File_attribute_reparse_point = 1024x768 (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 is set (0x100 + 0x20 = 0x120.)
3. Python gets file hidden properties via Win32API
A brief description of Win32API on the Python website https://www.python.org/download/windows/
Download Address http://sourceforge.net/projects/pywin32/

Copy the Code code as follows:

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)


Operation Result:

4. and arithmetic (&) more intuitive to judge hidden files
The example code is as follows the result of the,& operation corresponds to the hidden property value, which makes it more intuitive to determine the file type.
Copy the Code code as follows:

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)

Operation Result:

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.