How to read a bunch of files in a directory in lua

I have a path (as a string) to a directory. There are a bunch of text files in this directory. I want to go to that directory, open it and go to each text file and read the data.

I tried

f = io.open(path)
f:read("*a")

      

I get the error "nil is a directory"

I tried:

f = io.popen(path)

      

I get the error: "Permission denied"

It's just me, but it seems like it is much more complicated than doing the main io file in lua?

+3


source to share


2 answers


The directory is not a file. You can't just open it.

And yes, lua itself has (intentionally) limited functionality.



You can use luafilesystem or luaposix and similar modules to get more options in this area.

+4


source


You can also use the following script to list the filenames in a given directory (assuming Unix / Posix):



dirname = '.'
f = io.popen('ls ' .. dirname)
for name in f:lines() do print(name) end

      

+4


source







All Articles