Today’s Erlang Thursday 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("junk"). % true filelib:is_file("tmp"). % true filelib:is_file("tempmp"). % false filelib:is_file("temp"). % true filelib:is_file("/usr/local/bin"). % true filelib:is_file("/usr/local/var"). % true filelib:is_file("/usr/local/vars"). % 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(junk). % true filelib:is_file(["/usr", ['/local', '/bin']]). % true
–Proctor