How can I check if a file exists using 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.
To delete the file, use os. remove(path) . But close the file first.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With