我正在编写一个Mac OS程序,我有以下几行:
os.execute("cd ~/testdir")
configfile = io.open("configfile.cfg", "w")
configfile:write("hello")
configfile:close()
Run Code Online (Sandbox Code Playgroud)
问题是,它只在脚本当前目录中创建配置文件,而不是我刚刚进入的文件夹.我意识到这是因为我使用控制台命令来更改目录,然后指示Lua代码来编写文件.为了解决这个问题,我将代码更改为:
configfile = io.open("~/testdir/configfile.cfg", "w")
Run Code Online (Sandbox Code Playgroud)
但是我得到以下结果:
lua: ifontinst.lua:22: attempt to index global 'configfile' (a nil value)
stack traceback:
ifontinst.lua:22: in main chunk
Run Code Online (Sandbox Code Playgroud)
我的问题是,使用IO.Open在我刚刚在用户主目录中创建的文件夹中创建文件的正确方法是什么?
我很欣赏我在这里犯了一个新手的错误,所以如果你浪费你的时间我会道歉.