Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a file exists with Lua

How can I check if a file exists using Lua?

like image 279
Yoni Avatar asked Feb 14 '11 10:02

Yoni


People also ask

What is assert Lua?

Lua assert is the function to use in the error handling of the Lua programming language. This is the function helping to handle the runtime error of the Lua source code to avoid complications between compile-time error and run time error.

How do I delete a file in Lua?

To delete the file, use os. remove(path) . But close the file first.

How does require work in Lua?

Lua offers a higher-level function to load and run libraries, called require . Roughly, require does the same job as dofile , but with two important differences. First, require searches for the file in a path; second, require controls whether a file has already been run to avoid duplicating the work.


1 Answers

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.

like image 103
lhf Avatar answered Oct 07 '22 12:10

lhf