How to create a directory in Lua?

青春壹個敷衍的年華 提交于 2020-01-12 13:56:26

问题


Is it possible to create a directory in lua ? If so, how ?


回答1:


There's a "system" call (or something like that, this is from memory) which you should be able to use to run an arbitrary program, which could include the mkdir command.

EDIT: I found my Programming in Lua book. On page 203, it mentions how you could use an

os.execute("mkdir " .. dirname)

to "fake" a directory creation command.

EDIT 2: Take note of Jonas Thiem's warning that this command can be abused if the directory name comes from an untrusted source!




回答2:


You may find the LuaFileSystem library useful. It has a mkdir function.

require "lfs"
lfs.mkdir("/path/to/dir")



回答3:


You may also want to look at Lua/APR, the Apache Portable Runtime binding for Lua. The docs can be found at here

One of the reasons I use Lua is that I can write code that runs across multiple OSes. I was using LFS for some time, but have found that using Lua/APR provides a more platform-neutral library. And there are lots of other useful routines in the APR.




回答4:


You can use the paths package instead. Then you can simply do:

require 'paths'

paths.mkdir('your/dir')


来源:https://stackoverflow.com/questions/1690913/how-to-create-a-directory-in-lua

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!