lua-users home
lua-l archive

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


Is this 'trapping' the error that would otherwise stop Lua running? Does this work by directing the error report to a variable instead of allowing Lua to report directly and stop the program?


Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> wrote:
(01/09/2004 15:14)

>
>> 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