code: def ensure_dir_exists(cls, filepath):
          
          directory = os.path.dirname(filepath)
          if directory != '' and not os.path.exists(directory):
              try:
                  os.makedirs(directory)
              except OSError as e:
                  if e.errno != errno.EEXIST:
                      raise
docs: Ensure that a directory exists
  
          If it doesn't exist, try to create it and protect against a race condition
          if another process is doing the same.