lua-users home
lua-l archive

Re: How do I test for the existens of a file

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


2008年10月24日 Robert Raschke <rtrlists@googlemail.com>:
> On Fri, Oct 24, 2008 at 2:20 PM, TheLonelyStar <nabble2@lonely-star.org> wrote:
>> I want to test if a specific file exist, and do something if yes. Like
>>
>> if <file exist> then
>> <do something>
>> else
>> <do something else>
>> end
>>
>> How do I do that?
>
> I use
>
> function file_exists(file_name)
> local file = io.open(file_name)
> if file then
> file:close()
> end
> return file ~= nil
> end
If you have access to LuaFileSystem [1] you can also use:
function file_exist(file_name)
 return lfs.attributes(file_name, 'mode')~=nil
end
This may or may not be faster depending on your setup.
[1] http://www.keplerproject.org/luafilesystem/

AltStyle によって変換されたページ (->オリジナル) /