Lua 全局变量包含当前文件的路径? [英] Lua global variable containing path to current file?

查看:27
本文介绍了Lua 全局变量包含当前文件的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Lua 中是否有包含当前正在解释的文件路径的全局变量?类似于 Python 的 __file__ 变量?

Is there a global variable in Lua that contains the path to the file currently being interpreted? Something like Python's __file__ variable?

我在解释器中运行了一个快速的for k, v in pair(_G) do print(k) end 看看我是否能找到任何东西.只列出了以下变量?

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 pair rawget loadstring ipairs _VERSION dofile setfenv load error loadfile

关于如何获取当前正在执行的文件的路径有什么建议吗?

Any suggestions on how to get the path the file currently being executed?

推荐答案

调试库有一个您可以调用的 getinfo 方法,该方法可以返回函数的源文件等.

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);

这将在调用堆栈的第一级返回函数的源文件的名称(它将以@ 符号开头,表示它是一个文件名).通过传递 1 你是在询问有关当前函数的信息.如果您传入 0,它将返回 =[C],因为它将返回有关 getinfo 函数本身的信息.

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.

有关更多详细信息,请查看 Lua 官方网站上的 Lua 编程 参考:http://www.lua.org/pil/23.1.html

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆