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: github.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:ceabeda3-05c3-0080-973c-75ab574fff65 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B696:EA765:2C69BD:3D760A:696A4C9B |
| html-safe-nonce | 6153ed091a95cf17a6acf689a3482af1c65f70f571372fe08e1d8b79727fa8c6 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNjk2OkVBNzY1OjJDNjlCRDozRDc2MEE6Njk2QTRDOUIiLCJ2aXNpdG9yX2lkIjoiMjM2NjU2NTQ4MTk1MTQxNTQ1MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 569496eca85690eff1bc927ec22e72b2fba55308c0d6e56d2f82cbd388bc1978 |
| 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 | 3f871c8e07f0ae1886fa8dac284166d28b09ad5bada6476fc10b674e489788ef |
| 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 | 63c426b30d262aba269ef14c40e3c817b384cd61 |
| ui-target | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width