9951 explained code solutions for 126 technologies


luaHow to check if file exists


We'll use io.open to try to open file and return true or false based on this attempt:

local file_exists = io.open('/tmp/test.txt', "r") ~= nilctrl + c
file_exists

will contain true if file exists, otherwise false

io.open

opens file handler

/tmp/test.txt

file to check existence of

~= nil

compare value with nil (returned if there's no file)


Usage example

local file_exists = io.open('/tmp/test.txt', "r") ~= nil
print(file_exists)

local file_exists = io.open('unknow.txt', "r") ~= nil
print(file_exists)
output
true
false