Take the 2-minute tour ×

When using file:read_file(x) is it necessary to close the returned file?

{ok, File} = file:read_file("maillog.sample"), 
share|improve this question
File contains the contents of the file... –  Zed Jan 8 '10 at 7:26
add comment

2 Answers

up vote 9 down vote accepted

It is not a file, but the contents of the file that is returned. Thus, no file to close. Try changing the variable name to Data or similar as in the code below:

{ok, Data} = file:read_file("maillog.sample"),

Data will then have the contents of the file "maillog.sample". The function file:read_file/1 will open, read and close the file for you, all in one go.

share|improve this answer
Wow im dumb, since the return tuple is similar to output of file:open(..) I just assumed it was giving back a device. Thanks –  jdc0589 Jan 10 '10 at 1:37
add comment

No it is not necessary to close the files for file:read_file, and file:consult.

How would you close it anyway, if you don't even have the file descriptor at hand?

share|improve this answer
add comment

Your Answer


