lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> Please can someone tell me if Lua can tell if a file exists?

The only ANSI C way to do this is to open it:

	function exists(n)
		local f=io.open(n)
		io.close(f)
		return f==nil
	end

> I've tried testing for for a valid return from io.open(), but if the file doesn't exist, Lua throws up a default error, never mind trying to run my own handling code...

It works for me:

% lua
Lua 5.0  Copyright (C) 1994-2003 Tecgraf, PUC-Rio
> print(io.open"/etc/passwd")
file (0x80648b8)
> print(io.open"/etc/Passwd"); print"ok"
nil     /etc/Passwd: No such file or directory  2
ok
--lhf