I know this is an old topic, but maybe it will save some time to someone.
I was just having the same problem. Turns out I was calling
where path had the file name in it. But this actually creates a folder with a name the filename.
So the exception message was true: it was indeed a directory, not a file.