Skip to content

Not using with to open files - what about pathlib's read_text #139

Open
@DanGolding

Description

@DanGolding

Regarding the anti-pattern of not using with to open files, if you are reading the entire contents of the file in one shot as in the examples, you can use read_text from pathlib which opens and closes the file for you internally. So it isn't always an anti-pattern to not use a context manager for reading text/bytes, in fact I would argue that

with open("file.txt", "r") as f:
    content = f.read()

is an anti-pattern for

content  = Path("file.txt").read_text()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions