包含当前文件路径的Lua全局变量? [英] Lua global variable containing path to current file?
问题描述
__ file __
变量? 我为k,v成对运行了一个快速 (_G)在解释器中打印(k)end
,看是否能找到任何东西。只有下列变量被列出?
字符串xpcall包tostring打印操作系统unpack要求getfenv setmetatable下一个assert tonumber io rawequal collectgarbage getmetatable模块rawset数学调试pcall table newproxy type coroutine _G select gcinfo pairs rawget loadstring ipairs _VERSION dofile setfenv load error loadfile
有关如何获取当前文件路径的任何建议被执行?
调试库有一个 getinfo
调用,它可以返回一个函数的源文件。
local info = debug.getinfo(1, 'S');
print(info.source);
这将返回源文件的名称(以@符号开头,表示它是一个文件名)的函数在调用堆栈的第一级。通过传递 1
,您正在询问有关当前函数的信息。如果你传递了 0
,它会返回 = [C]
,因为它会返回关于 getinfo
函数本身。
有关更多详细信息,请查看 Lua编程
参考官方Lua网站:
http://www.lua.org/pil /23.1.html
Is there a global variable in Lua that contains the path to the file currently being interpreted? Something like Python's __file__
variable?
I ran a quick for k, v in pairs(_G) do print(k) end
in the interpreter to see if I could find anything. Only the following variables were listed?
string xpcall package tostring print os unpack require getfenv setmetatable next assert tonumber io rawequal collectgarbage getmetatable module rawset math debug pcall table newproxy type coroutine _G select gcinfo pairs rawget loadstring ipairs _VERSION dofile setfenv load error loadfile
Any suggestions on how to get the path the file currently being executed?
The debug library has a getinfo
method you can call, which can return, amongst other things, the source file for a function.
local info = debug.getinfo(1,'S');
print(info.source);
That would return the name of the source file (which will begin with an @ symbol, indicating it is a filename) of the function at the first level of the call stack. By passing 1
you are asking for information about the current function. If you passed in 0
it would return =[C]
as it would be returning information about the getinfo
function itself.
For more detailed information check out the Programming in Lua
reference on the official Lua website:
http://www.lua.org/pil/23.1.html
这篇关于包含当前文件路径的Lua全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!