Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
DanGolding opened this issue Dec 20, 2019 · 2 comments
Open

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

DanGolding opened this issue Dec 20, 2019 · 2 comments

Comments

@DanGolding
Copy link

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()
@abhijeetmote
Copy link

abhijeetmote commented Dec 25, 2019

Hi @DanGolding
I have done this, please let me know If I need to change anything. This is my first commit to this repo
#140

@DanGolding
Copy link
Author

Hi @DanGolding
I have done this, please let me know If I need to change anything. This is my first commit to this repo
#140

Looks good, thanks :) , left a couple of comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants