回答
問題是,庫存Lua分發(幾乎)只包含標準C中指定的功能。標準C不會假定存在實際存在任何特定分類的文件系統(甚至操作系統,因此os
和io
模塊不提供標準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)
重命名命名
oldname
到newname
文件。 如果此功能失敗,則返回 零,並加上描述 錯誤的字符串。
你可以嘗試設置使用oldName和newname一個相同的 - 你可能沒有寫權限,雖然如此,因爲你不能寫它可能會失敗,即使你可以讀。在這種情況下,您必須解析返回的錯誤字符串,並推斷您是否可以寫入,或者您必須嘗試執行需要現有文件的函數,並將其包裝在pcall
中。
如果您對避免LFS庫特別感興趣,Lua Posix library有一個stat()的接口。
require 'posix'
function isdir(fn)
return (posix.stat(fn, "type") == 'directory')
end
這裏有一個簡單的方法來檢查,如果存在,無需任何外部庫的依賴:)
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
好運:)
這是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用戶來說,這可能是一個解決方案:)
我使用這些(但實際上我檢查錯誤):
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的。 如果你不關心性能(和錯誤處理-.-),你可以使用它。
玩得開心編碼!
對於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
非常聰明的使用'os.rename' – 2017-12-31 16:38:57
- 1. 如何檢查目錄是否存在?
- 2. 檢查目錄是否不存在
- 3. Laravel s3檢查目錄是否存在。
- 4. SVNKit - 檢查目錄是否存在
- 5. Talend - 檢查目錄是否存在
- 6. NSIS檢查是否存在空目錄
- 7. 如何檢查目錄下是否存在特定目錄?
- 8. 檢查目錄是否存在,使目錄
- 9. 檢查表中是否存在記錄
- 10. Perl - 檢查目錄中是否存在數組中的文件
- 11. 如何在Perl中檢查Windows上是否存在Unicode目錄?
- 12. 如何在shell腳本中檢查目錄是否存在?
- 13. 在Meteor公共目錄中檢查文件是否存在
- 14. 檢查項目是否存在保存
- 15. Visual Studio安裝項目 - 檢查目錄是否存在
- 16. 檢查目錄中是否存在多個文件
- 17. 如何檢查Swift文檔目錄中是否存在文件?
- 18. 檢查目錄中是否存在兩個文件
- 19. 檢查文件是否存在於指定的目錄中
- 20. 用通配符檢查Perl中的目錄是否存在
- 21. 如何檢查目錄中是否存在文件?
- 22. 如何檢查傀儡中是否存在目錄
- 23. 檢查目錄中是否存在多個文件
- 24. 檢查文件是否保存在文檔目錄中
- 25. 檢查文件是否存在於特定目錄的shell中
- 26. 如何檢查Xcode資產目錄中是否存在圖像?
- 27. 如何檢查目錄是否存在於linux中。?
- 28. 檢查目錄中是否存在圖像和縮略圖
- 29. 如何檢查目錄是否已經存在於MFC(VC++)中?
- 30. 檢查目錄中是否存在多個文件
儘管如此,LuaPosix庫在Unix和Windows之間要大得多,不能移植。 – 2016-10-22 02:05:32