2009-08-27 214 views
11

如何檢查lua中是否存在目錄,如果可能,最好不要使用LuaFileSystem模塊?檢查lua中是否存在目錄?

試圖做這樣的事情蟒蛇行:

os.path.isdir(path) 

回答

12

問題是,庫存Lua分發(幾乎)只包含標準C中指定的功能。標準C不會假定存在實際存在任何特定分類的文件系統(甚至操作系統,因此osio模塊不提供標準C庫中不可用的訪問信息。

如果你試圖用純C標準編碼,你會遇到同樣的問題。

您可以通過嘗試使用它來了解該文件夾是否存在隱式存在。如果你期望它存在並且對你可寫,那麼在那裏創建一個臨時文件,如果成功,文件夾就存在。如果失敗,當然,您可能無法區分不存在的文件夾和權限不足。

到目前爲止,獲得特定答案的最輕量級答案將僅僅是提供所需信息的那些特定於操作系統的函數調用的精簡綁定。如果您可以接受lua alien模塊,那麼您可以像使用純粹的Lua那樣進行綁定。

更簡單但稍重一點的是接受Lua文件系統。它提供了一個便攜式模塊,支持大多數人想了解的文件和文件系統。

5

好了,5.1的參考手冊沒有在the os table任何東西,但如果你使用Nixstaller,你os.fileexists爲你解釋正是。

如果你能負擔得起一點點,或者如果你知道你將在哪個操作系統上運行,你可能會用標準os庫os.execute帶走一些系統調用來識別文件是否存在。

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

os.rename(oldname, newname)

重命名命名oldnamenewname文件。 如果此功能失敗,則返回 零,並加上描述 錯誤的字符串。

你可以嘗試設置使用oldName和newname一個相同的 - 你可能沒有寫權限,雖然如此,因爲你不能寫它可能會失敗,即使你可以讀。在這種情況下,您必須解析返回的錯誤字符串,並推斷您是否可以寫入,或者您必須嘗試執行需要現有文件的函數,並將其包裝在pcall中。

8

如果您對避免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

0

這裏有一個簡單的方法來檢查,如果存在,無需任何外部庫的依賴:)

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

2

這是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

1

我使用這些(但實際上我檢查錯誤):

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

0

對於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 
4

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

然後做:

require 'paths' 

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

我首選在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 
6

這是一種既能在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