code: def ensure_dir(filename):
      
      directory = os.path.dirname(filename)
      if directory and not os.path.isdir(directory):
          os.makedirs(directory)
docs: Checks if the dir of f exists, otherwise create it.