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
bpo-41737: [doc] add error handling example to file open section in IO tutorial #22818
Conversation
Co-authored-by: kj <28750310+Fidget-Spinner@users.noreply.github.com>
IMO this isn't a good fit in this part of the tutorial. When just introducing Also, not every call to Note that this part of the tutorial does include a prominent link to the docs for open(), in which the second sentence is "If the file cannot be opened, an OSError is raised." Having said that, this part of the tutorial does go into quite a bit of detail, and there can be room to mention error cases and handling. Adding mention later in this section that OSError is raised when errors happen, such as the file or parent dir not existing, seems like a good idea to me. |
I agree. Let me rework this to add the error handling later on. |
Doc/tutorial/inputoutput.rst
Outdated
>>> try: | ||
... f = open('workfile', 'w') | ||
... except OSError as e: | ||
... print('Cannot open file: ', e) | ||
... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this our recommended way of doing this? Don't we recommend using with open(...) as f:
? I'm thinking that this example, while short, clear and concise, should show the recommended approach.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point. At the same time, I usually prefer try-except blocks to be specific rather than around a large code block like the whole with and everything inside it. What is the recommended way to do open with error handling?
The reason I made this PR was because I felt that the OP of bpo-41737 wasn't clear that the API contract is OSError. Maybe this is not the way to clarify this point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the recommended way to do open with error handling?
Good question! Maybe the above, and then with f: ...
? Note that the very next section recommends with open(...) as f:
as "good practice".
Maybe this is not the way to clarify this point.
I think this is definitely the right place to show how things should be done, though perhaps after the paragraph introducing using a "with" statement with open().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are some examples here https://docs.python.org/3/tutorial/errors.html
I don't remember what this was about, but it seems stuck - closing as part of my spring clean. |
Tutorial currently doesn't show that OSError is what should be caught for file open errors.
https://bugs.python.org/issue41737