Check if a file exists with Lua

Yoni picture Yoni · Feb 14, 2011 · Viewed 66.8k times · Source

How can I check if a file exists using Lua?

Answer

lhf picture lhf · Feb 14, 2011

Try

function file_exists(name)
   local f=io.open(name,"r")
   if f~=nil then io.close(f) return true else return false end
end

but note that this code only tests whether the file can be opened for reading.