How to determine windows hidden files using python

Source: Internet
Author: User
The hidden file in inux starts with ".". you can determine whether the file is a hidden file based on the file name. Win is determined by the file hiding attribute. Therefore, you can only obtain the hidden attribute through Microsoft API to determine whether the object is a hidden object. 1. use the windows attrib command to obtain the file hiding attribute

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




2. hide the property value and its meaning
Constants-the following attribute values are returned by the GetFileAttributes function:

The code is as follows:


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_normally = 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 uses win32api to obtain the file hiding attribute
Python official website for win32API simple description https://www.python.org/download/windows/
Http://sourceforge.net/projects/pywin32/

The code is 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)


Running result:

4. more intuitive judgment of hidden files with operations (&)
The sample code is as follows. the calculation result corresponds to the hidden property value, allowing you to more intuitively determine the file type.

The code is 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)

Running 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.