Check if Asset Exists

Is there a way to check if an asset exists in the assets directory? app.get_asset_url() returns a URL, regardless of whether there’s a file behind it or not. I’m trying to write a validator to help other devs when launching apps in our environment

2 Likes

+1, I would also like to know this. @ponteaus did you ever find a solution for this?

I am interested as well !

Not really a dash specific thing, i would use os

import os

def check_file_exists(folder_path, file_name):
  """
  Checks if a file exists in a given folder.

  Args:
    folder_path: The path to the folder.
    file_name: The name of the file to check for.

  Returns:
    True if the file exists, False otherwise.
  """
  file_path = os.path.join(folder_path, file_name)
  return os.path.exists(file_path)

# Example usage:
folder_path = "/path/to/your/folder"  # Replace with the actual folder path
file_name = "your_file.txt"  # Replace with the actual file name

if check_file_exists(folder_path, file_name):
  print(f"The file '{file_name}' exists in the folder '{folder_path}'.")
else:
  print(f"The file '{file_name}' does not exist in the folder '{folder_path}'.")