Skip to content

Returns:

Type Description
FileData

JSON, or an iterator of JSON records, if the file is JSON Lines

Parameters:

Name Type Description Default
path FilePath

the string or path to the file to read

required
Source code in fil/fil.py
42
43
44
45
46
47
48
49
50
51
52
53
def read(path: FilePath) -> FileData:
    """
    Reads data from a file based on its suffix

    Returns:
       JSON, or an iterator of JSON records, if the file is JSON Lines

    Args:
      path: the string or path to the file to read
    """
    p = Path(path)
    return _get_class(p).read(p)

Writes data to a file with a format based on the file's suffix.

Parameters:

Name Type Description Default
data FileData

JSON, or an iterator of JSON records, if the file is JSON Lines

required
path FilePath

the string or path to the file to read

required
use_safer Optional[bool]

whether to use the safer module to avoid writing incomplete files. The default, None means to use safer on all files except for JSON Line files, where you want each line to be immediately written as it appears, and a partial file saved if there is a crash

None
kwargs Dict

named arguments passed to the underlying writer

{}
Source code in fil/fil.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def write(
    data: FileData,
    path: FilePath,
    *,
    use_safer: Optional[bool] = None,
    **kwargs: Dict,
) -> None:
    """
    Writes data to a file with a format based on the file's suffix.

    Args:
        data: JSON, or an iterator of JSON records, if the file is JSON Lines

        path: the string or path to the file to read

        use_safer: whether to use the safer module to avoid writing incomplete
            files. The default, `None` means to use `safer` on all files *except*
            for JSON Line files, where you want each line to be immediately written
            as it appears, and a partial file saved if there is a crash

        kwargs: named arguments passed to the underlying writer
    """
    p = Path(path)
    return _get_class(p).write(data, p, use_safer=use_safer, **kwargs)

About this project