def __init__(self, key, sub_key, flag):
      """
Constructor :
   key     : HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS
   sub_key : The name of a key that this method opens
   flag    : "r" for read
             "w" for write
             "a" for append
             "b" for binary
      """
      from win32api import RegQueryValueEx, RegCreateKey, RegOpenKeyEx
      from win32con import KEY_ALL_ACCESS
      from StringIO import StringIO
      
      # Save files informations
      self.key = key
      self.sub_key = sub_key
      
      # Initialization of internal data
      self.deleted = 0
      self.handle = None
      self.data = None
      
      # Check mode
      self.__checkMode(flag)
      
      # Create an empty file
      self.data = StringIO()
      
      try:
         # Open registry key
         self.handle = RegOpenKeyEx(key,sub_key,0,KEY_ALL_ACCESS)
         
         # Read key content
         data = RegQueryValueEx(self.handle, "")[0]
         
         # If the file is in read mode
         if self.flag == self.READ:
            # Read data
            self.data = StringIO(data)
         # If the file is in append mode
         elif self.flag == self.APPEND:
            self.data.write (data)
      except:  # If the registry key not found
         # If the file must be read
         if self.flag == self.READ:
            # The file is not found
            raise FileNotFoundError(key,sub_key)
         else:
            # Create a new registry key
            self.handle = RegCreateKey(key, sub_key)

Constructor :
   key     : HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS
   sub_key : The name of a key that this method opens
   flag    : "r" for read
             "w" for write
             "a" for append
             "b" for binary
      
recipes/Python/163969_Win32_registry_file/recipe-163969.py