检查lua中是否存在目录?

问题描述:

如何检查lua中是否存在目录,如果可能,最好不要使用LuaFileSystem模块?检查lua中是否存在目录?

试图做这样的事情蟒蛇行:

os.path.isdir(path) 

问题是,库存Lua分发(几乎)只包含标准C中指定的功能。标准C不会假定存在实际存在任何特定分类的文件系统(甚至操作系统,因此osio模块不提供标准C库中不可用的访问信息。

如果你试图用纯C标准编码,你会遇到同样的问题。

您可以通过尝试使用它来了解该文件夹是否存在隐式存在。如果你期望它存在并且对你可写,那么在那里创建一个临时文件,如果成功,文件夹就存在。如果失败,当然,您可能无法区分不存在的文件夹和权限不足。

到目前为止,获得特定答案的最轻量级答案将仅仅是提供所需信息的那些特定于操作系统的函数调用的精简绑定。如果您可以接受lua alien模块,那么您可以像使用纯粹的Lua那样进行绑定。

更简单但稍重一点的是接受Lua文件系统。它提供了一个便携式模块,支持大多数人想了解的文件和文件系统。

好了,5.1的参考手册没有在the os table任何东西,但如果你使用Nixstaller,你os.fileexists为你解释正是。

如果你能负担得起一点点,或者如果你知道你将在哪个操作系统上运行,你可能会用标准os库os.execute带走一些系统调用来识别文件是否存在。

甚至比os.execute可能os.rename:

os.rename(oldname, newname)

重命名命名oldnamenewname文件。 如果此功能失败,则返回 零,并加上描述 错误的字符串。

你可以尝试设置使用oldName和newname一个相同的 - 你可能没有写权限,虽然如此,因为你不能写它可能会失败,即使你可以读。在这种情况下,您必须解析返回的错误字符串,并推断您是否可以写入,或者您必须尝试执行需要现有文件的函数,并将其包装在pcall中。

如果您对避免LFS库特别感兴趣,Lua Posix library有一个stat()的接口。

require 'posix' 
function isdir(fn) 
    return (posix.stat(fn, "type") == 'directory') 
end 
+0

尽管如此,LuaPosix库在Unix和Windows之间要大得多,不能移植。 – 2016-10-22 02:05:32

这里有一个简单的方法来检查,如果存在,无需任何外部库的依赖:)

function directory_exists(path) 
    local f = io.popen("cd " .. path) 
    local ff = f:read("*all") 

    if (ff:find("ItemNotFoundException")) then 
    return false 
    else 
    return true 
    end 
end 

print(directory_exists("C:\\Users")) 
print(directory_exists("C:\\ThisFolder\\IsNotHere")) 

如果您复制并粘贴到上述Lua的一个文件夹,你应该看到

false 
true 

好运:)

+3

此解决方案似乎假定用户正在使用Powershell ...但是,检查cd命令的返回代码可能会起作用。 – djs 2013-01-26 19:48:29

+0

我期待第一个结果是真实的,第二个结果是假的。 – RAL 2013-05-28 16:56:08

这是Windows平台上进行测试。这实际上很容易:

local function directory_exists(sPath) 
    if type(sPath) ~= "string" then return false end 

    local response = os.execute("cd " .. sPath) 
    if response == 0 then 
    return true 
    end 
    return false 
end 

显然,这可能不适用于其他操作系统。但对于Windows用户来说,这可能是一个解决方案:)

+0

当我在linux和windows上测试时,无论'sPath'的存在,'response'都是'nil'? – Alex 2017-11-09 22:14:54

+0

对于像这样简单的任务调用外部程序通常不被认为是一种好的做法,因为这样做往往会造成尽可能多的问题。在类Unix操作系统中,应该引用'sPath'参数(不确定Windows),这很难做到。 – Lassi 2017-11-27 12:41:43

我使用这些(但实际上我检查错误):

require("lfs") 
-- no function checks for errors. 
-- you should check for them 

function isFile(name) 
    if type(name)~="string" then return false end 
    if not isDir(name) then 
     return os.rename(name,name) and true or false 
     -- note that the short evaluation is to 
     -- return false instead of a possible nil 
    end 
    return false 
end 

function isFileOrDir(name) 
    if type(name)~="string" then return false end 
    return os.rename(name, name) and true or false 
end 

function isDir(name) 
    if type(name)~="string" then return false end 
    local cd = lfs.currentdir() 
    local is = lfs.chdir(name) and true or false 
    lfs.chdir(cd) 
    return is 
end 

os.rename(1,名称2)将重命名名1至名2。使用相同的名称并且不应该改变(除了有一个坏蛋错误)。如果一切顺利,返回true,否则返回nil和errormessage。你说你不想使用lfs。如果你不试图打开文件(这有点慢,但确定)不能区分文件和导演。

因此,没有LuaFileSystem

-- no require("lfs") 

function exists(name) 
    if type(name)~="string" then return false end 
    return os.rename(name,name) and true or false 
end 

function isFile(name) 
    if type(name)~="string" then return false end 
    if not exists(name) then return false end 
    local f = io.open(name) 
    if f then 
     f:close() 
     return true 
    end 
    return false 
end 

function isDir(name) 
    return (exists(name) and not isFile(name)) 
end 

它看起来短,但需要更长的时间...... 另外打开一个文件是存在风险的,因为你应该使用LFS的。 如果你不关心性能(和错误处理-.-),你可以使用它。

玩得开心编码!

+3

'io.open'也适用于目录。您需要额外尝试读取它('f:read(1)')。 – blueyed 2015-04-05 12:52:25

+0

谢谢!不知道!如果文件为空(如果我添加'f:read(1)'),那么这也不会引发错误?而AFAIK的'f:read(0)'也不会帮助... – tDwtp 2015-07-23 02:46:28

对于Linux用户:

function dir_exists(path) 
if type(path) ~= 'string' then 
    error('input error') 
    return false 
end 
local response = os.execute('cd ' .. path) 
if response == nil then 
    return false 
end 
return response 
end 

您也可以使用 '路径' 包。 Here的Lua中链接到包

然后做:

require 'paths' 

if paths.dirp('your_desired_directory') then 
    print 'it exists' 
else 
    print 'it does not exist' 
end 

我首选在linux这样做的方式是

if os.execute '[ -e "/home" ]' then 
    io.write "it exists" 
    if os.execute '[ -d "/home" ]' then 
    io.write " and is a directory" 
    end 
    io.write "\n" 
end 

,或者把这个变成一个功能:

function is_dir(path) 
    return os.execute(('[ -d "%s" ]'):format(path)) 
end -- note that this implementation will return some more values 

这是一种既能在Unix和Windows上工作,又不需要任何外部依赖的方式:

--- Check if a file or directory exists in this path 
function exists(file) 
    local ok, err, code = os.rename(file, file) 
    if not ok then 
     if code == 13 then 
     -- Permission denied, but it exists 
     return true 
     end 
    end 
    return ok, err 
end 

--- Check if a directory exists in this path 
function isdir(path) 
    -- "/" works on both Unix and Windows 
    return exists(path.."/") 
end 
+1

非常聪明的使用'os.rename' – 2017-12-31 16:38:57