Yet another "check if CSV file exists" cry for help...

I’ve only been using Knime version 5.2 for a few days, so I’m still a complete beginner when it comes to the “how”, “where” and “why”.
I’ve done my homework, read the documentation and looked through the supposedly relevant articles on the subject, some of which date back to 2016.
Here is my problem: Read a CSV file. In the event that it does not exist, create an empty CSV file with this name and pass on an empty table.

So far, so simple.

My approaches with Try / Catch, a case differentiation with “If / Then” and even a recourse to the Java method “exists()” from the java.io.File class have all failed with the message of the CSV Node “Execute failed: The specified file xyz.csv does not exist.”.

Oh, if only there was a file reader with two outputs for success/failure!

I am grateful for any advice.

Hi,
I usually check for files by using a List Files/Folders node combined with a Row Filter. After the filter, you can use an Empty Table Switch to activate one of two branches depending on whether the file is there or not. You can merge the branches back together with a CASE Switch End.
Kind regards,
Alexander

In this screenshot I show how you could do it. The Extract Variables nodes are just there to convert the table port to a flow variable port. They do nothing to the data, really.

4 Likes

Thank you very much for your answer, which has put me on the right track. Your idea with the “Extract Variables” as an entry point for the reader was spot on.

I have now implemented a slightly modified way with Try/Catch. As I need an entry point anyway, I have put an empty table in front of it, but it already has the same structure as the CSV file. The catch statement now either accepts the existing data from the CSV file or the previously generated empty table.

Problem solved. Thank you very much!

2 Likes

Hi,
Are you sure this works if the file is missing? As far as I know, the CSV Reader fails during configuration when the file is missing and this is not caught by the Try-Catch block. It could be that this was changed in recent AP versions, though.
Kind regards,
Alexander

Yes, tested under all limit conditions and found to be good. I was lucky that I only started with version 5 :sunglasses:
Gruß
Denis

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.