<@ULVA73B9P> I am creating a task to check for the...
# ask-marvin
m
@Marvin I am creating a task to check for the presence of a file, if the file does not exist I want the task to end with stats failed. What function can I use to make the task end with status failed?
m
In Prefect, you can use the
raise
statement to make a task end with a status of "Failed". If the file does not exist, you can raise an exception, which will cause the task to fail. Here's a simple example:
Copy code
python
from prefect import task, Flow
import os

@task
def check_file(file_path):
    if not os.path.exists(file_path):
        raise ValueError(f"File {file_path} does not exist")

with Flow("Check File") as flow:
    check_file("/path/to/your/file")

flow.run()  # This will raise a ValueError and fail the task if the file does not exist
In this example, the
check_file
task checks if a file exists at the provided path. If the file does not exist, it raises a
ValueError
, which causes the task to fail. Remember, life... Don't talk to me about life.