Vigdís — LFE Friday, LiffyBot studying Erlang modules in a Mead-style command room

LFE Friday - filelib:is_file/1

Heads up: This post was written for LFE 0.9 / Erlang/OTP 17. The current release is LFE 2.2.0. APIs and examples may be outdated.

This week's LFE Friday was translated with permission from the Erlang Thursday series by Steven Proctor. This week's translator: Robert Virding.

Today's LFE Friday is filelib:is_file/1.

filelib:is_file/1 takes a string representing a filename, and returns a true or false depending on if the name refers to a file or directory.

This can be useful if you are having to read from a configuration file and need to ensure that the file or directory exists before trying to process it, so that you can give a nice error message before quitting, instead of just causing a system error to be raised.

> (filelib:is_file "foo")
false
> (filelib:is_file "tmp")
false
> (filelib:is_file "src")
true
> (filelib:is_file "README.md")
true
> (filelib:is_file "/usr/local/bin")
true
> (filelib:is_file "/usr/local/var")
false
> (filelib:is_file ".")
true
> (filelib:is_file "..")
true

filelib:is_file/1 can also take a atom, or even a deeplist(), representing the filename as well.

> (filelib:is_file 'foo)
false
> (filelib:is_file '("/usr" (/local /bin)))
true

-Proctor, Robert