Working with files

Lua can create, rename, and delete files. We've already seen how to create files in the File IO section of this chapter. You can use io.open to create a new file:

local file = io.open("new_file.txt", "a")
file:close()

Files can be renamed with the os.rename function. This function takes two arguments: the path to the file to be renamed and the path to the final, renamed version of the file. The code here renames the file created in the last sample:

os.rename("new_file.txt", "renamed_file.txt")

Files can be deleted with the os.remove function. This function takes the path of a file to delete. To remove the previously renamed file, you would have to use the following code:

os.remove("renamed_file.txt")

Lua can not natively create, rename, enumerate or delete folders, like it can files. All of these functions, however, do exist in each operating system. Lua can use os.execute to perform these actions. For example, you could make a directory on any platform, like so:

os.execute("mkdir new_folder")
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.168.214