Skip to content

JSON CLI: I/O operation on closed file" when parsing JSON Lines file #132631

Closed

Description

@hugovk

Bug report

Bug description:

With any JSON Lines file, such as:

❯ cat input.jsonl
{"ingredients":["frog", "water", "chocolate", "glucose"]}
{"ingredients":["chocolate","steel bolts"]}

This works on Python 3.12:

❯ python3.12 --version
Python 3.12.10

❯ python3.12 -m json.tool --json-lines input.jsonl
{
    "ingredients": [
        "frog",
        "water",
        "chocolate",
        "glucose"
    ]
}
{
    "ingredients": [
        "chocolate",
        "steel bolts"
    ]
}

But not 3.13 or higher:

❯ python3.13 --version
Python 3.13.3

❯ python3.13 -m json.tool --json-lines input.jsonl
I/O operation on closed file.

❯ python3.14 --version
Python 3.14.0a7

❯ python3.14 -m json.tool --json-lines input.jsonl
I/O operation on closed file.

❯ ./python.exe --version --version
Python 3.14.0a7+ (heads/main:e42bda94411, Apr 17 2025, 14:59:38) [Clang 16.0.0 (clang-1600.0.26.6)]

❯ ./python.exe Lib/test/test_json/test_tool.py -h

❯ ./python.exe -m json.tool --json-lines input.jsonl
I/O operation on closed file.

CPython versions tested on:

3.13, 3.14, CPython main branch

Operating systems tested on:

macOS, Linux

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directory
    3.13only security fixes
    3.14bugs and security fixes
    on Apr 17, 2025
  2. nineteendo commented on Sep 16, 2025

    @nineteendo
  3. mpkocher commented on Sep 16, 2025

    @mpkocher
    Contributor

    There's also an issue with the "in place" functionality. This is captured in a unit test, but there's no mention of this requirement or functionality in the code.

    In place can overwrite the current file, but requires special attention.

    python -m json.tool example.json example.json

    For the in place functionality, it should probably write to a tmp file, then atomically overwrite the original file and delete the tmp file.

    The use case of in place and --json-lines is a bit odd (needs --no-indent), but it still should mechanically work and not error out.

    >> cat a.jline                                                
    {"alpha": 123,                     "beta": 122345}
    {"alpha": 124,       "beta": 456}

    From a file, using --no-indent can cleanup a file.

    >> python  -m json.tool --no-indent --json-lines a.jline 
    {"alpha": 123, "beta": 122345}
    {"alpha": 124, "beta": 456}

    In place will break and overwrite the original file and leave an empty file, effectively deleting(!) the original file.

    >> cat a.jline       
    {"alpha": 123,                     "beta": 122345}
    {"alpha": 124,       "beta": 456}
    >> python -m json.tool --json-lines --no-indent a.jline a.jline
    I/O operation on closed file.
    >> cat a.jline 
    >>
  4. added 3 commits that reference this issue on Apr 23, 2026
  5. added a commit that references this issue on Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13only security fixes3.14bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions