@@ -7,7 +7,7 @@ The file module provides access to the file system and its individual files.
...
@@ -7,7 +7,7 @@ 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 subdirectories/folders.
The file system is a flat file system, with no notion of subdirectories/folders.
Besides the SPIFFS file system on internal flash, this module can also access FAT partitions on an external SD card is[FatFS is enabled](../sdcard.md).
Besides the SPIFFS file system on internal flash, this module can also access FAT partitions on an external SD card if[FatFS is enabled](../sdcard.md).
```lua
```lua
-- open file in flash:
-- open file in flash:
...
@@ -32,6 +32,10 @@ Change current directory (and drive). This will be used when no drive/directory
...
@@ -32,6 +32,10 @@ Change current directory (and drive). This will be used when no drive/directory
Current directory defaults to the root of internal SPIFFS (`/FLASH`) after system start.
Current directory defaults to the root of internal SPIFFS (`/FLASH`) after system start.
!!! note
Function is only available when [FatFS support](../sdcard.md#enabling-fatfs) is compiled into the firmware.
#### Syntax
#### Syntax
`file.chdir(dir)`
`file.chdir(dir)`
...
@@ -73,7 +77,9 @@ end
...
@@ -73,7 +77,9 @@ end
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.
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.
Not supported for SD cards.
!!! note
Function is not supported for SD cards.
#### Syntax
#### Syntax
`file.format()`
`file.format()`
...
@@ -91,7 +97,9 @@ none
...
@@ -91,7 +97,9 @@ none
Returns the flash address and physical size of the file system area, in bytes.
Returns the flash address and physical size of the file system area, in bytes.
Not supported for SD cards.
!!! note
Function is not supported for SD cards.
#### Syntax
#### Syntax
`file.fscfg()`
`file.fscfg()`
...
@@ -156,7 +164,9 @@ end
...
@@ -156,7 +164,9 @@ end
Mounts a FatFs volume on SD card.
Mounts a FatFs volume on SD card.
Not supported for internal flash.
!!! note
Function is only available when [FatFS support](../sdcard.md#enabling-fatfs) is compiled into the firmware and it is not supported for internal flash.
#### Syntax
#### Syntax
`file.mount(ldrv[, pin])`
`file.mount(ldrv[, pin])`
...
@@ -217,7 +227,7 @@ When done with the file, it must be closed using `file.close()`.
...
@@ -217,7 +227,7 @@ When done with the file, it must be closed using `file.close()`.
`file.open(filename, mode)`
`file.open(filename, mode)`
#### Parameters
#### Parameters
-`filename` file to be opened, directories are not supported
Get attribtues of a file or directory in a table. Elements of the table are:
-`size` file size in bytes
-`size` file size in bytes
-`name` file name
-`name` file name
-`time` table with time stamp information. Default is 1970-01-01 00:00:00 in case time stamps are not supported (on SPIFFS).
-`time` table with time stamp information. Default is 1970-01-01 00:00:00 in case time stamps are not supported (on SPIFFS).
-`year`
-`mon`
- `year`
-`day`
- `mon`
-`hour`
- `day`
-`min`
- `hour`
-`sec`
- `min`
- `sec`
-`is_dir` flag `true` if item is a directory, otherwise `false`
-`is_dir` flag `true` if item is a directory, otherwise `false`
-`is_rdonly` flag `true` if item is read-only, otherwise `false`
-`is_rdonly` flag `true` if item is read-only, otherwise `false`
-`is_hidden` flag `true` if item is hidden, otherwise `false`
-`is_hidden` flag `true` if item is hidden, otherwise `false`
...
@@ -387,8 +399,7 @@ end
...
@@ -387,8 +399,7 @@ end
The maximum number of open files on SPIFFS is determined at compile time by `SPIFFS_MAX_OPEN_FILES` in `user_config.h`.
The maximum number of open files on SPIFFS is determined at compile time by `SPIFFS_MAX_OPEN_FILES` in `user_config.h`.
## file.close()
## file.close(), file.obj:close()
## file.obj:close()
Closes the open file, if any.
Closes the open file, if any.
...
@@ -406,10 +417,9 @@ none
...
@@ -406,10 +417,9 @@ none
#### See also
#### See also
[`file.open()`](#fileopen)
[`file.open()`](#fileopen)
## file.flush()
## file.flush(), file.obj:flush()
## file.obj:flush()
Flushes any pending writes to the file system, ensuring no data is lost on a restart. Closing the open file using [`file.close()` / `fd:close()`](#fileclose) performs an implicit flush as well.
Flushes any pending writes to the file system, ensuring no data is lost on a restart. Closing the open file using [`file.close()` / `fd:close()`](#fileclose-fileobjclose) performs an implicit flush as well.
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 1024, this function only returns the first 1024 bytes.
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 1024, this function only returns the first 1024 bytes.