title: How do I unlock locked files and folders (mac) with Python
body: <p>As a &quot;clean up&quot; after my script's main purpose is complete, a function is called to recursively look through each folder and remove all files that end in a pre-determined set of extensions.</p>
  <p>I during my testing, I discovered that some files with a file extension in the list of ones to delete actually throw an error: <code>[Errno 1] Operation not permitted: '/location/of/locked/file.png</code>. Looking at the file itself, it appears to be <em>Locked</em> (on mac).</p>
  <ol>
  <li><strong>How would I go about removing the locked attribute (should it exist) from every file/folder using Python, then delete the file if it ends in the extension?</strong><br />
  Preferably this can all be done in the same function below, as it takes a long time to traverse the input directory - handling each only once is the way to go.</li>
  <li><strong>How does this affect the script's integrity on Windows?</strong><br />
  I have taken care of programming it in a way that makes it compatible between the OSs but (to my knowledge) the <em>locked</em> attribute does not exist on Windows the way it does on mac and could cause unknown side-effects.</li>
  </ol>
  <hr />
  <pre><code>REMOVE_FILETYPES = ('.png', '.jpg', '.jpeg', '.pdf')
  
  def cleaner(currentPath):
    if not os.path.isdir(currentPath):
      if currentPath.endswith(REMOVE_FILETYPES) or os.path.basename(currentPath).startswith('.'):
        try:
          os.remove(currentPath)
          print('REMOVED: \&quot;{removed}\&quot;'.format(removed = currentPath))
        except BaseException as e:
          print('ERROR: Could not remove: \&quot;{failed}\&quot;'.format(failed = str(e)))
        finally:
          return True
      return False
      
    if all([cleaner(os.path.join(currentPath, file)) for file in os.listdir(currentPath)]):
      try:
        os.rmdir(currentPath)
        print('REMOVED: \&quot;{removed}\&quot;'.format(removed = currentPath))
      except:
        print('ERROR: Could not remove: \&quot;{failed}\&quot;'.format(failed = currentPath))
      finally:
        return True
    return False
  
  cleaner(r'/path/to/parent/dir')
  </code></pre>
  <p>I would really appreciate if somebody could show me how to integrate such functionality into the sub-routine. Cheers.</p>
  <hr />
  <p><strong>EDIT:</strong> Removed error handling as per request</p>
  <pre><code>def cleaner(currentPath):
      if sys.platform == 'darwin':
          os.system('chflags nouchg {}'.format(currentPath))
      if not os.path.isdir(currentPath):
          if currentPath.endswith(REMOVE_FILETYPES) or os.path.basename(currentPath).startswith('.'):
              try:
                  os.remove(currentPath)
                  print('REMOVED: \&quot;{removed}\&quot;'.format(removed=currentPath))
              except PermissionError:
                  if sys.platform == 'darwin':
                      os.system('chflags nouchg {}'.format(currentPath))
                      os.remove(currentPath)
      if all([cleaner(os.path.join(currentPath, file)) for file in os.listdir(currentPath)]) and not currentPath == SOURCE_DIR:
          os.rmdir(currentPath)
          print('REMOVED: \&quot;{removed}\&quot;'.format(removed=currentPath))
  </code></pre>