code: def ensure_parent_directory(path, ensure_parent=True):
      
      parent_directory = os.path.abspath(path)
      if ensure_parent:
          parent_directory = os.path.dirname(parent_directory)
      if not os.path.exists(parent_directory):
          try:
              os.makedirs(parent_directory)
          except (IOError, OSError):
              raise OSError(u"Directory '%s' cannot be created" % parent_directory)
docs: Ensures the parent directory exists.
  
      :param string path: the path of the file
      :param bool ensure_parent: if ``True``, ensure the parent directory of ``path`` exists;
                                 if ``False``, ensure ``path`` exists
      :raises: OSError: if the path cannot be created