Macos – Io.Open – Lua cannot be used in the main catalog

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

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

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

But 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 correct way to use IO.Open to create files in the folder I just created in the user’s home directory?

I appreciate that I made a novice mistake here, so I will apologize if you waste your time.

< /div>

You have a problem with the ~ symbol. In your os.execute(“cd~/testdir”) is the shell to interpret the symbol and replace it with your home path. However, in io. In open(“~/testdir/configfile.cfg”, “w”), Lua receives the string and Lua does not interpret this symbol, so your program will try to open the file in the incorrect folder. A simple solution Is to call os.getenv(“HOME”) and connect the path string with the file path:

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

In order to improve the error message, I suggest you use the assert() function to wrap io.open():

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

I am writing a Mac OS program, 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 is only in The configuration file is created in the current directory of the script, not the folder I just entered. I realized this was because I used console commands to change the directory and then instructed Lua code to write the file. To solve this problem, I changed the code to :

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

But 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 problem is that using IO.Open in the user main What is the correct way to create files in a folder created in a directory?

I appreciate that I made a novice mistake here, so I will apologize if you waste your time.

You There is a problem with the ~ symbol. In your os.execute(“cd~/testdir”) is the shell to interpret the symbol and replace it with your home path. However, in io.open(“~/testdir/configfile.cfg”, “W”), Lua receives the string and Lua does not interpret this symbol, so your program will try to open the file in the incorrect folder. A simple solution is to call os.getenv(“HOME”) and The path string is connected with the file path:

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

In order to improve the error message, I suggest you use the assert() function to wrap io.open():

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

Leave a Comment

Your email address will not be published.