如何在lua中检查目录是否存在,最好不使用LuaFileSystem模块?
尝试像Python代码这样实现:
os.path.isdir(path)
如何在lua中检查目录是否存在,最好不使用LuaFileSystem模块?
尝试像Python代码这样实现:
os.path.isdir(path)
这是一种适用于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
os
和io
模块不提供标准C库中无法获得的访问信息。require 'posix'
function isdir(fn)
return (posix.stat(fn, "type") == 'directory')
end
os.fileexists
,完全符合您所解释的内容。os.execute
函数以及识别文件是否存在的某些系统调用来实现。os.execute
更好的方法是os.rename:
您可以尝试将oldname和newname设置为相同--但是您可能没有写入权限,因此它可能会由于无法写入而失败,即使您可以阅读。 在这种情况下,您必须解析返回的错误字符串并推断是否可以写入,或者您必须尝试执行需要现有文件的函数,并将其包装在
os.rename(oldname, newname)
将名为
oldname
的文件重命名为newname
。 如果此函数失败,则返回nil
,以及描述错误的字符串。
pcall
中。require 'paths'
if paths.dirp('your_desired_directory') then
print 'it exists'
else
print 'it does not exist'
end
我使用这些(但实际上我会检查错误):
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(name1, name2)会将name1重命名为name2。如果使用相同的名称,除了出现错误之外,什么也不会改变。如果一切顺利,它将返回true,否则它将返回nil和错误消息。您说您不想使用lfs。如果不使用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。如果您不关心性能(和错误处理-.-),那么可以直接使用它。
编写愉快!
io.open
同样适用于目录。您需要尝试读取它(f:read(1)
)才能实现。 - blueyed这是针对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
sPath
是否存在,response
都是nil
? - AlexsPath
参数进行引号包裹(不确定Windows),这很难正确完成。 - Lassi以下是一种简单的方法,可以检查文件夹是否存在,而不需要任何外部库依赖 :)
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"))
false
true
祝你好运 :)
not directory_exists("C:\\ThisFolder\\IsNotHere")
- Fox这个感觉很简单,我觉得没人做的原因可能是有点难以理解。
但它在我的机器上(Ubuntu focal)可以为文件和目录工作。
然而,它会说存在但被禁止的文件不存在。
对于快速脚本或少量文件:
function exists(path)
return (io.open(path,"r") ~= nil)
end
好的实践:
function exists(path)
local file = io.open(path,"r")
if (file ~= nil) then
io.close(file)
return true
else
return false
end
end
针对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
os.rename
进行那个操作。 - Lucas Siqueiraos.rename
而不是相对路径,以使你的代码更加稳定。 - VimNing