title: How to read or write the a s h r i file attributes on Windows using Python and ctypes?
body: <p>For the records:</p>
  
  <ul>
  <li><code>a</code> means 'archivable'</li>
  <li><code>s</code> means 'system'</li>
  <li><code>h</code> means 'hidden'</li>
  <li><code>r</code> means 'readonly'</li>
  <li><code>i</code> means 'indexable'</li>
  </ul>
  
  <p>My current solution to read/write these attributes from Python scripts is to call <code>attrib</code> using the <a href="https://docs.python.org/3/library/subprocess.html#module-subprocess" rel="nofollow noreferrer">subprocess</a> module.</p>
  
  <p><strong>Python code</strong>:</p>
  
  <pre><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))))
  </code></pre>
  
  <p><strong>Output</strong>:</p>
  
  <pre><code>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}
  </code></pre>
  
  <p>But this performs slow when the directory contains many entries (one subprocess call per entry).</p>
  
  <p>I dont want to use the <a href="http://code.activestate.com/recipes/303343-changing-file-attributes-on-windows/" rel="nofollow noreferrer">win32api</a> module because i dont want third party module dependencies. Also im curious how to do it with <a href="https://docs.python.org/3/library/ctypes.html#module-ctypes" rel="nofollow noreferrer">ctypes</a>.</p>
  
  <p>I have stumbled over <a href="https://stackoverflow.com/questions/549109/hide-folders-file-with-python">Hide Folders/ File with Python [closed]</a>, <a href="https://stackoverflow.com/questions/19622133/set-hide-attribute-on-folders-in-windows-os">Set "hide" attribute on folders in windows OS?</a> and <a href="https://stackoverflow.com/questions/16326395/python-windows-system-file">Python: Windows System File</a>, but this is not clear to me. Especially i dont understand what these 0x4 es 0x02 es are. Can you explain this? Can you give a concrete code example?</p>