Title: Not using with to open files - what about pathlib's read_text · Issue #139 · quantifiedcode/python-anti-patterns · GitHub
Open Graph Title: Not using with to open files - what about pathlib's read_text · Issue #139 · quantifiedcode/python-anti-patterns
X Title: Not using with to open files - what about pathlib's read_text · Issue #139 · quantifiedcode/python-anti-patterns
Description: 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 i...
Open Graph Description: 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 clo...
X Description: 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 clo...
Opengraph URL: https://github.com/quantifiedcode/python-anti-patterns/issues/139
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Not using with to open files - what about pathlib's read_text","articleBody":"Regarding the anti-pattern of [not using with to open files](https://docs.quantifiedcode.com/python-anti-patterns/maintainability/not_using_with_to_open_files.html), if you are reading the entire contents of the file in one shot as in the examples, you can use [`read_text`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.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\r\n\r\n```python\r\nwith open(\"file.txt\", \"r\") as f:\r\n content = f.read()\r\n```\r\n\r\nis an anti-pattern for\r\n\r\n```python\r\ncontent = Path(\"file.txt\").read_text()\r\n```","author":{"url":"https://github.com/DanGolding","@type":"Person","name":"DanGolding"},"datePublished":"2019-12-20T11:07:02.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/139/python-anti-patterns/issues/139"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:e7a0221f-8034-8c8a-a47b-eed4ba79c204 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D388:8B50E:48756C0:5E1AD41:69754FC6 |
| html-safe-nonce | 75acab1437bdde1e27ab147aee44769f77aa3415a1a44efc09d458eaa46f7da6 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMzg4OjhCNTBFOjQ4NzU2QzA6NUUxQUQ0MTo2OTc1NEZDNiIsInZpc2l0b3JfaWQiOiI1NjA0MjkzMDQ3MjMyMDU3Mjg2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 50d31f516d79cc6738a5f25ad2558762efc6526dc9383053bea31e4ec9eede4a |
| hovercard-subject-tag | issue:540939345 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/quantifiedcode/python-anti-patterns/139/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a81f0a025b96717af94366f76c4a5b908d3192bec9f20b75a515fdd6815b5c49/quantifiedcode/python-anti-patterns/issues/139 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a81f0a025b96717af94366f76c4a5b908d3192bec9f20b75a515fdd6815b5c49/quantifiedcode/python-anti-patterns/issues/139 |
| og:image:alt | 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 clo... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | DanGolding |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4a4bf5f4e28041a9d2e5c107d7d20b78b4294ba261cab243b28167c16a623a1f |
| turbo-cache-control | no-preview |
| go-import | github.com/quantifiedcode/python-anti-patterns git https://github.com/quantifiedcode/python-anti-patterns.git |
| octolytics-dimension-user_id | 6774260 |
| octolytics-dimension-user_login | quantifiedcode |
| octolytics-dimension-repository_id | 22258112 |
| octolytics-dimension-repository_nwo | quantifiedcode/python-anti-patterns |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 22258112 |
| octolytics-dimension-repository_network_root_nwo | quantifiedcode/python-anti-patterns |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 488b30e96dfd057fbbe44c6665ccbc030b729dde |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width