Skip to main content

load_file

Loads data from a file path by reading its bytes and delegating to load_str_bytes, with automatic protocol detection based on file extensions like .json or .pkl.

def load_file(
path: str | Path,
content_type: str | None = None,
encoding: str = 'utf8',
proto: Protocol | None = None,
allow_pickle: bool = False,
json_loads: Callable[[str], Any] = json.loads
) - > Any

Loads and parses the content of a file from a given path based on its extension or a specified protocol.

Parameters

NameTypeDescription
path`strPath`
content_type`strNone` = None
encodingstr = 'utf8'The character encoding used to decode the file bytes into a string.
proto`ProtocolNone` = None
allow_picklebool = FalseWhether to allow the loading of pickled data, which can be a security risk.
json_loadsCallable[[str], Any] = json.loadsThe JSON loading function to use when parsing JSON content.

Returns

TypeDescription
AnyThe deserialized data from the file, such as a dictionary from JSON or a Python object from a pickle file.