filefox
is a simple helper toolbox for reading and writing files.
pip install filefox
pip install git+https://github.com/Yevgnen/filefox.git
Function | Description |
---|---|
read_json |
Reading Json file |
write_json |
Writing Json file |
read_pickle |
Reading Pickle file |
write_pickle |
Writing Pickle file |
read_toml |
Reading Toml file |
write_toml |
Writing Toml file |
read_toml |
Reading text file |
write_toml |
Writing text file |
read |
Detect file extension, decompress when necessary, read file |
write |
Detect file extension, write file, compress when necessary |
These functions have the following convention of their signatures:
reader(filename, ..., file_kwargs=None, ...)
writer(obj, filename, ..., file_kwargs=None, ...)
- For all readers, the first positional argument is always the filename.
- For all writers, the first positional argument is always the dump object and the second is filename.
- All functions accpet a keyword argument
file_kwargs
which is an optionaldict
will be unpacked and passed to file opening function, e.g.open
. The other keyword arguments will be passed to internal load/dump functions.
# -*- coding: utf-8 -*-
import filefox
obj = {
"name": "John",
"age": 12,
}
filefox.write_json(obj, "json_data.json")
assert obj == filefox.read_json("json_data.json")
filefox.write_json(obj, "json_data.json.gz")
assert obj == filefox.read_json("json_data.json.gz")
filefox.write(obj, "json_data.json")
assert obj == filefox.read("json_data.json")
filefox.write(obj, "json_data.json.gz")
assert obj == filefox.read("json_data.json.gz")
filefox.write_json(
obj, "json_data.json", file_kwargs={"mode": "w", "encoding": "utf8"}, indent=2
)
assert obj == filefox.read_json("json_data.json", file_kwargs={"encoding": "utf8"})
To ensure the codebase complies with a style guide, please use flake8, black and isort tools to format and check codebase for compliance with PEP8.