`MMEngine` implements a unified set of file reading and writing interfaces in `fileio` module. With the `fileio` module, we can use the same function to handle different file formats, such as `json`, `yaml` and `pickle`. Other file formats can also be easily extended.
The `fileio` module also supports reading and writing files from a variety of file storage backends, including disk, Petrel (for internal use), Memcached, LMDB, and HTTP.
## Load and dump data
`MMEngine` provides a universal API for loading and dumping data, currently supported formats are `json`, `yaml`, and `pickle`.
### Load from disk or dump to disk
```python
from mmengine import load, dump
# load data from a file
data = load('test.json')
data = load('test.yaml')
data = load('test.pkl')
# load data from a file-like object
with open('test.json', 'r') as f:
data = load(f, file_format='json')
# dump data to a string
json_str = dump(data, file_format='json')
# dump data to a file with a filename (infer format from file extension)
dump(data, 'out.pkl')
# dump data to a file with a file-like object
with open('test.yaml', 'w') as f:
data = dump(data, f, file_format='yaml')
```
### Load from other backends or dump to other backends
```python
from mmengine import load, dump
# load data from a file
data = load('s3://bucket-name/test.json')
data = load('s3://bucket-name/test.yaml')
data = load('s3://bucket-name/test.pkl')
# dump data to a file with a filename (infer format from file extension)
dump(data, 's3://bucket-name/out.pkl')
```
It is also very convenient to extend the API to support more file formats. All you need to do is to write a file handler inherited from `BaseFileHandler` and register it with one or several file formats.
```python
from mmengine import register_handler, BaseFileHandler
# To register multiple file formats, a list can be used as the argument.