Python's method for judging windows hidden files _python

Source: Internet
Author: User
Tags in python

1. Get file Hide properties via Windows attrib command

Copy Code code 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

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 are returned by the GetFileAttributes function:

Copy 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 = 128 (0x80)
File_attribute_temporary = 256 (0x100)
File_attribute_sparse_file = 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, 0x120 indicates the temporary + Archive attributes are set (0x100 + 0x20 = 0x120.)
3. Python gets file hidden properties via Win32API
A brief description of Win32API in Python's official website https://www.python.org/download/windows/
Download Address http://sourceforge.net/projects/pywin32/

Copy 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)


Run Result:

4. With operations (&) more intuitive to determine hidden files
Example code the following,& the result of the operation and the hidden attribute value, can be more intuitive to determine the file type.

Copy 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)

Run Result:

Related Article

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.