我当前正在尝试使用打开文件 io.open(文件名,“ r”)
我的目录结构是
parent_dir
-src
-main.lua
-tests
-test.txt
我目前正在尝试使用以下方法从main.lua中的tests文件夹中打开txt文件 io.open(file_name,“ r”),但无法将文件识别为现有文件。如果我将绝对路径放入file_name中,它将起作用。我不确定如何格式化相对路径?我尝试了某些版本的“ ../../../tests/test.txt”,但似乎无法正常工作。 文件的实际路径类似于/Users/user_name/parent_dir/tests/test.txt
谢谢
答案 0 :(得分:0)
The ../ syntax is correct to specify relative paths.
But this is not relative to the location of your Lua script but to your current working directory.
Refer to get current working directory in Lua
You cannot change the current working directory from within a Lua script unless you use libraries like LuaFileSystem.
If you're running a single script you can check if global arg[0]
(if it is not nil) contains the path of that script. You can use that to build an absolute path from your script's location.