For the records:
a
means 'archivable's
means 'system'h
means 'hidden'r
means 'readonly'i
means 'indexable'
My current solution to read/write these attributes from Python scripts is to call attrib
using the subprocess module.
Python code:
import os, subprocess
def attrib(path, a=None, s=None, h=None, r=None, i=None):
attrs=[]
if r==True: attrs.append('+R')
elif r==False: attrs.append('-R')
if a==True: attrs.append('+A')
elif a==False: attrs.append('-A')
if s==True: attrs.append('+S')
elif s==False: attrs.append('-S')
if h==True: attrs.append('+H')
elif h==False: attrs.append('-H')
if i==True: attrs.append('+I')
elif i==False: attrs.append('-I')
if attrs: # write attributes
cmd = attrs
cmd.insert(0,'attrib')
cmd.append(path)
cmd.append('/L')
return subprocess.call(cmd, shell=False)
else: # just read attributes
output = subprocess.check_output(
['attrib', path, '/L'],
shell=False, universal_newlines=True
)[:9]
attrs = {'A':False, 'S':False, 'H':False, 'R':False, 'I':False}
for char in output:
if char in attrs:
attrs[char] = True
return attrs
path = 'C:\\test\\'
for thing in os.listdir(path):
print(thing, str(attrib(os.path.join(path,thing))))
Output:
archivable.txt {'A': True, 'I': False, 'S': False, 'H': False, 'R': False}
hidden.txt {'A': True, 'I': False, 'S': False, 'H': True, 'R': False}
normal.txt {'A': True, 'I': False, 'S': False, 'H': False, 'R': False}
readonly.txt {'A': True, 'I': False, 'S': False, 'H': False, 'R': True}
system.txt {'A': True, 'I': False, 'S': True, 'H': False, 'R': False}
But this performs slow when the directory contains many entries (one subprocess call per entry).
I don't want to use the win32api module because I don't want third party module dependencies. Also, I'm curious how to do it with ctypes.
I have stumbled over Hide Folders/ File with Python [closed], Set "hide" attribute on folders in windows OS? and Python: Windows System File, but this is not clear to me. Especially, I don't understand what these 0x4 es 0x02 es are. Can you explain this? Can you give a concrete code example?