Files


When I created my firmware using https://nodemcu-build.com/ I added the following modules: file, gpio, http, mqtt, net, node, tmr, uart, wifi and tls. I will select the commands I find relevant from each module and study them here, starting with "file". I use nodemcu.readthedocs.io as my reference.

I will only study the "Basic model" where there is only one file opened at a time.

Create a file and write text to it

file.open() , file.write() , file.close() and file.list()

To create a file named "myfirst.txt", you must open it with the right mode, then write text to it and finally close it. If the file does not exist, file.open() creates it.

The modes for opening it are:

  • "r": read mode (the default)
  • "w": write mode
  • "a": append mode
  • "r+": update mode, all previous data is preserved
  • "w+": update mode, all previous data is erased
  • "a+": append update mode, previous data is preserved, writing is only allowed at the end of file

So, in ESPlorer, type and send :

file.open ('myfirst.txt', 'w')
file.write('This text goes inside my file')
file.close()

Now, use the snippet I mentioned at the "First steps" chapter to list the files in filesystem:

> for k,v in pairs(file.list()) do l = string.format("%-15s",k) print(l.." "..v.." bytes") end
myfirst.txt     29 bytes
>

I could have named the file "myfirst.lua" or just "myfirst". NodeMcu doesn't care about file extensions.

file.writeline()

Write a string to the open file and append '\n' at the end.

file.open ('mysecond.txt', 'w')
file.writeline('First line')
file.writeline('Second line')
file.writeline('Third line')
file.close()

Reading a text file

file.read()

Same principle, very straightforward:

file.open ('myfirst.txt')  --read is default
a= file.read()
file.close()
print(a)
This text goes inside my file

file.readline()

file.open ('mysecond.txt') --read is default
a= file.readline()
b= file.readline()
c= file.readline()
file.close()
print(a)
print(b)
print(c)
First line
Second line
Third line

To do:


file.exists()

Determines whether the specified file exists.

file.format()

Format the file system.

file.remove()

Remove a file from the file system.

file.rename()

Renames a file.

file.flush()

Flushes any pending writes to the file system, ensuring no data is lost on a restart.

Things to study later (if ever):

file.chdir() Change current directory (and drive).

file.stat() Get attribtues of a file or directory in a table.

file.fscfg () Returns the flash address and physical size of the file system area, in bytes.

file.fsinfo() Return size information for the file system.

file.mount() Mounts a FatFs volume on SD card.

file.on() Registers callback functions.

file.seek() Sets and gets the file position, measured from the beginning of the file, to the position given by offset plus a base specified by the string whence.

Object model Files are represented by file objects which are created by file.

file.obj:close() Closes the open file, if any.

file.obj:flush() Flushes any pending writes to the file system, ensuring no data is lost on a restart.

file.obj:read() Read content from the open file.

file.obj:readline() Read the next line from the open file.

file.obj:seek() Sets and gets the file position, measured from the beginning of the file, to the position given by offset plus a base specified by the string whence.

file.obj:write() Write a string to the open file.

file.obj:writeline() Write a string to the open file and append '\n' at the end.

results matching ""

    No results matching ""