2016-01-05 02:21:19 +01:00
# file Module
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.
2016-01-10 16:04:23 +01:00
## file.close()
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
Closes the open file, if any.
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Syntax
`file.close()`
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Parameters
none
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Returns
`nil`
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Example
2016-01-05 02:21:19 +01:00
```lua
2016-01-10 16:04:23 +01:00
-- open 'init.lua', print the first line.
file.open("init.lua", "r")
print(file.readline())
file.close()
2016-01-05 02:21:19 +01:00
```
2016-01-10 16:04:23 +01:00
#### See also
[`file.open()` ](#fileopen )
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
## file.flush()
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
Flushes any pending writes to the file system, ensuring no data is lost on a restart. Closing the open file using [`file.close()` ](#fileclose ) performs an implicit flush as well.
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Syntax
`file.flush()`
#### Parameters
none
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Returns
2016-01-05 02:21:19 +01:00
`nil`
2016-01-10 16:04:23 +01:00
#### Example
2016-01-05 02:21:19 +01:00
```lua
2016-01-10 16:04:23 +01:00
-- 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()
2016-01-05 02:21:19 +01:00
```
2016-01-10 16:04:23 +01:00
#### See also
[`file.close()` ](#fileclose )
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
## file.format()
2016-01-07 00:33:52 +01:00
2016-01-10 16:04:23 +01:00
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.
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Syntax
`file.format()`
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Parameters
none
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Returns
2016-01-05 02:21:19 +01:00
`nil`
2016-01-10 16:04:23 +01:00
#### See also
[`file.remove()` ](#fileremove )
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
## file.fsinfo()
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
Return size information for the file system, in bytes.
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Syntax
`file.fsinfo()`
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Parameters
none
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Returns
- `remaining` (number)
- `used` (number)
- `total` (number)
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Example
2016-01-05 02:21:19 +01:00
```lua
2016-01-10 16:04:23 +01:00
-- get file system info
remaining, used, total=file.fsinfo()
print("\nFile system info:\nTotal : "..total.." Bytes\nUsed : "..used.." Bytes\nRemain: "..remaining.." Bytes\n")
2016-01-05 02:21:19 +01:00
```
2016-01-10 16:04:23 +01:00
## file.list()
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
Lists all files in the file system.
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Syntax
`file.list()`
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Parameters
none
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Returns
a lua table which contains the {file name: file size} pairs
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Example
2016-01-05 02:21:19 +01:00
```lua
2016-01-10 16:04:23 +01:00
l = file.list();
for k,v in pairs(l) do
print("name:"..k..", size:"..v)
end
2016-01-05 02:21:19 +01:00
```
2016-01-10 16:04:23 +01:00
2016-01-05 02:21:19 +01:00
## file.open()
Opens a file for access, potentially creating it (for write modes).
When done with the file, it must be closed using `file.close()` .
2016-01-10 16:04:23 +01:00
#### Syntax
2016-01-05 02:21:19 +01:00
`file.open(filename, mode)`
2016-01-10 16:04:23 +01:00
#### Parameters
- `filename` file to be opened, directories are not supported
- `mode` :
- "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
2016-01-05 02:21:19 +01:00
- "a+": append update mode, previous data is preserved, writing is only allowed at the end of file
2016-01-10 16:04:23 +01:00
#### Returns
`nil` if file not opened, or not exists (read modes). `true` if file opened ok.
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Example
2016-01-05 02:21:19 +01:00
```lua
2016-01-10 16:04:23 +01:00
-- open 'init.lua', print the first line.
file.open("init.lua", "r")
print(file.readline())
file.close()
2016-01-05 02:21:19 +01:00
```
2016-01-10 16:04:23 +01:00
#### See also
- [`file.close()` ](#fileclose )
- [`file.readline()` ](#filereadline )
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
## file.read()
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
Read content from the open file.
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Syntax
`file.read([n_or_str])`
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Parameters
- `n_or_str` :
- if nothing passed in, read all byte in file
- if pass a number n, then read n bytes from file, or EOF is reached
- if pass a string "str", then read until 'str' or EOF is reached
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Returns
fdile content in string, or nil when EOF
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Example
2016-01-05 02:21:19 +01:00
```lua
2016-01-10 16:04:23 +01:00
-- print the first line of 'init.lua'
file.open("init.lua", "r")
print(file.read('\n'))
file.close()
-- print the first 5 byte of 'init.lua'
file.open("init.lua", "r")
print(file.read(5))
file.close()
2016-01-05 02:21:19 +01:00
```
2016-01-07 00:33:52 +01:00
2016-01-10 16:04:23 +01:00
#### See also
- [`file.open()` ](#fileopen )
- [`file.readline()` ](#filereadline )
2016-01-05 02:21:19 +01:00
## file.readline()
Read the next line from the open file.
2016-01-10 16:04:23 +01:00
#### Syntax
2016-01-05 02:21:19 +01:00
`file.readline()`
2016-01-10 16:04:23 +01:00
#### Parameters
none
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Returns
2016-01-05 02:21:19 +01:00
File content in string, line by line, include EOL('\n'). Return `nil` when EOF.
2016-01-10 16:04:23 +01:00
#### Example
2016-01-05 02:21:19 +01:00
```lua
2016-01-10 16:04:23 +01:00
-- print the first line of 'init.lua'
file.open("init.lua", "r")
print(file.readline())
file.close()
2016-01-05 02:21:19 +01:00
```
2016-01-10 16:04:23 +01:00
#### See also
- [`file.open()` ](#fileopen )
- [`file.close()` ](#fileclose )
- [`file.read()` ](#filereade )
2016-01-07 00:33:52 +01:00
2016-01-10 16:04:23 +01:00
## file.remove()
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
Remove a file from the file system. The file must not be currently open.
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
###Syntax
`file.remove(filename)`
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Parameters
`filename` file to remove
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Returns
`nil`
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Example
2016-01-05 02:21:19 +01:00
```lua
2016-01-10 16:04:23 +01:00
-- remove "foo.lua" from file system.
file.remove("foo.lua")
2016-01-05 02:21:19 +01:00
```
2016-01-10 16:04:23 +01:00
#### See also
[`file.open()` ](#fileopen )
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
## file.rename()
2016-01-07 00:33:52 +01:00
2016-01-10 16:04:23 +01:00
Renames a file. If a file is currently open, it will be closed first.
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Syntax
`file.rename(oldname, newname)`
#### Parameters
- `oldname` old file name
- `newname` new file name
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Returns
`true` on success, `false` on error.
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Example
2016-01-05 02:21:19 +01:00
```lua
2016-01-10 16:04:23 +01:00
-- rename file 'temp.lua' to 'init.lua'.
file.rename("temp.lua","init.lua")
2016-01-05 02:21:19 +01:00
```
2016-01-10 16:04:23 +01:00
## 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.
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Syntax
`file.seek([whence [, offset]])`
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Parameters
- `whence`
- "set": base is position 0 (beginning of the file)
- "cur": base is current position (default value)
- "end": base is end of file
- `offset` default 0
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
If no parameters are given, the function simply returns the current file offset.
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Returns
the resulting file position, or `nil` on error
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Example
2016-01-05 02:21:19 +01:00
```lua
2016-01-10 16:04:23 +01:00
file.open("init.lua", "r")
-- skip the first 5 bytes of the file
file.seek("set", 5)
print(file.readline())
file.close()
2016-01-05 02:21:19 +01:00
```
2016-01-10 16:04:23 +01:00
#### See also
[`file.open()` ](#fileopen )
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
## file.write()
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
Write a string to the open file.
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Syntax
`file.write(string)`
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Parameters
`string` content to be write to file
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Returns
`true` if the write is ok, `nil` on error
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Example
2016-01-05 02:21:19 +01:00
```lua
2016-01-10 16:04:23 +01:00
-- 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()
2016-01-05 02:21:19 +01:00
```
2016-01-07 00:33:52 +01:00
2016-01-10 16:04:23 +01:00
#### See also
- [`file.open()` ](#fileopen )
- [`file.writeline()` ](#filewriteline )
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
## file.writeline()
Write a string to the open file and append '\n' at the end.
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Syntax
`file.writeline(string)`
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Parameters
`string` content to be write to file
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Returns
`true` if write ok, `nil` on error
2016-01-05 02:21:19 +01:00
2016-01-10 16:04:23 +01:00
#### Example
2016-01-05 02:21:19 +01:00
```lua
2016-01-10 16:04:23 +01:00
-- 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()
2016-01-05 02:21:19 +01:00
```
2016-01-07 00:33:52 +01:00
2016-01-10 16:04:23 +01:00
#### See also
- [`file.open()` ](#fileopen )
- [`file.readline()` ](#filereadline )