为什么这个Lua脚本无法打开Windows子目录?
2012-8-27 15:59:0
收藏:0
阅读:142
评论:1
我试图在 Lua 脚本中确定是否存在其中一个目录。它在 OSX 上运行正常,但在 Windows 上无法运行(Linux 目前未经测试,但我预计它可以运行)。当运行以下代码时,我会收到一个错误:
failed with this C:\Program Files (x86)\VideoLAN\VLC\lua\playlist\: No such file or directory
我可以确认那个目录存在。我已经转义了斜杠,我不知道还有什么问题。
local oses = { "/Applications/VLC.app/Contents/MacOS/share/lua/playlist/"; "C:\\Program Files\\VideoLAN\\VLC\\lua\\playlist\\"; "C:\\Program Files (x86)\\VideoLAN\\VLC\\lua\\playlist\\"; "/usr/lib/vlc/lua/playlist" }
-- 确定此操作系统(以及在哪里找到 share/lua)。
local f,err = io.open( oses[1], "r")
if not err then
opsys = "OSX"
scriptpath = oses[1] .. script
f:close()
else
f,err = io.open( oses[2], "r")
if not err then
opsys = "Win32"
scriptpath = oses[2] .. script
f:close()
else
f,err = io.open( oses[3], "r")
vlc.msg.dbg( dhead .. 'failed with this ' .. err .. dtail )
if not err then
opsys = "Win64"
scriptpath = oses[3] .. script
f:close()
else
f,err = io.open( oses[4], "r")
if not err then
opsys = "Linux/Unix"
scriptpath = oses[4] .. script
f:close()
else
return false
end
end
end
end
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
文件"C:\Program Files\VideoLAN\VLC\lua\playlist\"不存在。如果你删除行末的斜线,你将尝试打开一个目录,并且可能会遇到权限错误。无论哪种方式,它都不会起作用。如果你要使用这种确定操作系统的方法,应该尝试打开文件。
例如,构建你的脚本路径,尝试打开该文件,并使用它来确定通过/失败。
另外,你的代码结构可以大大改善。每当你有大量重复的代码,它们只是因为索引不同而不同,你应该使用循环。例如,我们可以使用以下代码替换你的代码:
local oses = { ["OSX"] = "/Applications/VLC.app/Contents/MacOS/share/lua/playlist/", ["Win32"] = "C:\\Program Files\\VideoLAN\\VLC\\lua\\playlist\\", ["Win64"] = "C:\\Program Files (x86)\\VideoLAN\\VLC\\lua\\playlist\\", ["Linux/Unix"] = "/usr/lib/vlc/lua/playlist", } for osname, directory in pairs(oses) do local scriptpath = directory..script local f,err = io.open( scriptpath, "r") if not err then f:close() return scriptpath, osname end end