In LuaFileSystem, lfs.dir(<path>)
iterates over the contents of a directory as strings. If I wanted to print the attributes of the contents of a directory, I might do it like this (using inspect.lua to print a human readable table representation):
for name in lfs.dir(<path>) do
local path = <path> .. '/' .. name
inspect(lfs.attributes(path))
end
However, the first two strings from the iterator seem to always be '.'
and '..'
(the shell notation for current directory and previous directory).
This causes errors in my code as <path> .. '/' .. '.'
is never a file or directory.
I've begun skipping these by advancing the state twice before looping.
local it, state = lfs.dir(path)
state:next()
state:next()
for file_name, _ in it, state, nil do
-- stuff
end
The advantage is that I do not have to explicitly check to see if the string is '.'
or '..'
for each iteration. One disadvantage is a decrease in readability.
Will there ever be a case where the first two strings are different?