Since | Origin / Contributor | Maintainer | Source |
---|---|---|---|
2014-12-22 | Zeroday | Zeroday | file.c |
The file module provides access to the file system and its individual files.
The file system is a flat file system, with no notion of directories/folders.
Only one file can be open at any given time.
Closes the open file, if any.
file.close()
none
nil
-- open 'init.lua', print the first line.
file.open("init.lua", "r")
print(file.readline())
file.close()
Determines whether the specified file exists.
file.exists(filename)
filename
file to checktrue of the file exists (even if 0 bytes in size), and false if it does not exist
files = file.list()
if files["device.config"] then
print("Config file exists")
end
if file.exists("device.config") then
print("Config file exists")
end
Flushes any pending writes to the file system, ensuring no data is lost on a restart. Closing the open file using file.close()
performs an implicit flush as well.
file.flush()
none
nil
-- open 'init.lua' in 'a+' mode
file.open("init.lua", "a+")
-- write 'foo bar' to the end of the file
file.write('foo bar')
file.flush()
-- write 'baz' too
file.write('baz')
file.close()
Format the file system. Completely erases any existing file system and writes a new one. Depending on the size of the flash chip in the ESP, this may take several seconds.
file.format()
none
nil
Returns the flash address and physical size of the file system area, in bytes.
file.fscfg()
none
flash address
(number)size
(number)print(string.format("0x%x", file.fscfg()))
Return size information for the file system, in bytes.
file.fsinfo()
none
remaining
(number)used
(number)total
(number)-- get file system info
remaining, used, total=file.fsinfo()
print("\nFile system info:\nTotal : "..total.." Bytes\nUsed : "..used.." Bytes\nRemain: "..remaining.." Bytes\n")
Lists all files in the file system.
file.list()
none
a lua table which contains the {file name: file size} pairs
l = file.list();
for k,v in pairs(l) do
print("name:"..k..", size:"..v)
end
Opens a file for access, potentially creating it (for write modes).
When done with the file, it must be closed using file.close()
.
file.open(filename, mode)
filename
file to be opened, directories are not supportedmode
:
nil
if file not opened, or not exists (read modes). true
if file opened ok.
-- open 'init.lua', print the first line.
file.open("init.lua", "r")
print(file.readline())
file.close()
Read content from the open file.
file.read([n_or_str])
n_or_str
:
LUAL_BUFFERSIZE
bytes (default 1024) or the entire file (whichever is smaller)n
bytes, LUAL_BUFFERSIZE
bytes, or EOF is reached. Specifying a number larger than the buffer size will read the buffer size.str
, then read until str
appears next in the file, LUAL_BUFFERSIZE
bytes have been read, or EOF is reachedFile content as a string, or nil when EOF
-- print the first line of 'init.lua'
file.open("init.lua", "r")
print(file.read('\n'))
file.close()
-- print the first 5 bytes of 'init.lua'
file.open("init.lua", "r")
print(file.read(5))
file.close()
Read the next line from the open file. Lines are defined as zero or more bytes ending with a EOL (’\n’) byte. If the next line is longer than LUAL_BUFFERSIZE
, this function only returns the first LUAL_BUFFERSIZE
bytes (this is 1024 bytes by default).
file.readline()
none
File content in string, line by line, including EOL(’\n’). Return nil
when EOF.
-- print the first line of 'init.lua'
file.open("init.lua", "r")
print(file.readline())
file.close()
Remove a file from the file system. The file must not be currently open.
###Syntax
file.remove(filename)
filename
file to remove
nil
-- remove "foo.lua" from file system.
file.remove("foo.lua")
Renames a file. If a file is currently open, it will be closed first.
file.rename(oldname, newname)
oldname
old file namenewname
new file nametrue
on success, false
on error.
-- rename file 'temp.lua' to 'init.lua'.
file.rename("temp.lua","init.lua")
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.seek([whence [, offset]])
whence
offset
default 0If no parameters are given, the function simply returns the current file offset.
the resulting file position, or nil
on error
file.open("init.lua", "r")
-- skip the first 5 bytes of the file
file.seek("set", 5)
print(file.readline())
file.close()
Write a string to the open file.
file.write(string)
string
content to be write to file
true
if the write is ok, nil
on error
-- open 'init.lua' in 'a+' mode
file.open("init.lua", "a+")
-- write 'foo bar' to the end of the file
file.write('foo bar')
file.close()
Write a string to the open file and append ‘\n’ at the end.
file.writeline(string)
string
content to be write to file
true
if write ok, nil
on error
-- open 'init.lua' in 'a+' mode
file.open("init.lua", "a+")
-- write 'foo bar' to the end of the file
file.writeline('foo bar')
file.close()