Cannot use io.open in home directory - Lua

I am writing a program for Mac OS and I have the following lines:

os.execute("cd ~/testdir")
configfile = io.open("configfile.cfg", "w")
configfile:write("hello")
configfile:close()

The problem is that it only creates the configuration file in the current script directory, and not in the folder in which I just entered cd '. I realized that this is because I use the console command to change the directory, and then direct the Lua code to write the file. To combat this, I changed the code to this:

configfile = io.open("~/testdir/configfile.cfg", "w")

However, I get the following result:

lua: ifontinst.lua:22: attempt to index global 'configfile' (a nil value)
stack traceback:
ifontinst.lua:22: in main chunk

My question is: what is the right way to use IO.Open to create a file in a folder that I just created in the users home directory?

I appreciate that I am making a rookie mistake here, so I apologize if you spend your time on me.

+4
1

~. os.execute("cd ~/testdir") , . io.open("~/testdir/configfile.cfg", "w") Lua, , Lua , . - os.getenv("HOME") :

configfile = io.open(os.getenv("HOME").."/testdir/configfile.cfg", "w")

, io.open() assert():

configfile = assert( io.open(os.getenv("HOME").."/testdir/configfile.cfg", "w") )
+5

Source: https://habr.com/ru/post/1614244/


All Articles