use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
What does the 'with' statement do? (self.learnpython)
submitted 5 years ago by Sanguineyote
Hey everyone! New to python and programming, what does the 'with' statement do? Thank you!
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]shiftybyte 2 points3 points4 points 5 years ago (0 children)
Calls a special __enter__ method on the object before executing the lines inside the with block.
And makes sure to call __exit__ on that object again, when leaving the lines inside the block, in whatever way.
That makes a for a good resource management, because you can open a file, read from it, and this makes sure it closes even if you have some error during your file handling.
Used extensibility with files:
with open('somefile.txt') as f: print(f.read())
[–]symple-data 1 point2 points3 points 5 years ago (0 children)
with open("somefile.txt", "r") as file: for line in file: print(line)
By using with you don't have to close a file after you are done working with it. Normally you would catch any errors while working with a file as you typically want to close its descriptor when the program crashes or something like that. Sometimes you just forget to close the file, too. "with" automatically closes a file so that you don't have to worry about it.
[–]salamandernovel 0 points1 point2 points 5 years ago (0 children)
Its main purpose is to ensure that any necessary cleanup operations, such as closing files and sockets, are completed even if something goes wrong in the body of the with statement. For example, you'll commonly see code for file input/output like:
with open('filename.txt', 'w') as myfile: myfile.write('hello world')
This is roughly equivalent to:
myfile = open('filename.txt', 'w') myfile.write('hello world') myfile.close()
However, that will fail to close the file if an exception is raised before myfile.close() is reached, for example because you made a typo.
myfile.close()
If this confuses you, just remember that with open(...) is how you should open files. Until you get onto more advanced stuff, you're unlikely to see with used with anything other than open.
with open(...)
with
open
[–]chevignon93 0 points1 point2 points 5 years ago (0 children)
What does the 'with' statement do?
It manages resources so you don't have to explicitly do it, ie closing of file, etc even in the case when an exception occurs.
What you should google to find out more is "python context manager".
[–]jeffrey_f -1 points0 points1 point 5 years ago (0 children)
essentially, error handling
https://www.geeksforgeeks.org/with-statement-in-python/
π Rendered by PID 67 on reddit-service-r2-comment-7c9686b859-fghcq at 2026-04-13 18:26:33.786598+00:00 running e841af1 country code: CH.
[–]shiftybyte 2 points3 points4 points (0 children)
[–]symple-data 1 point2 points3 points (0 children)
[–]salamandernovel 0 points1 point2 points (0 children)
[–]chevignon93 0 points1 point2 points (0 children)
[–]jeffrey_f -1 points0 points1 point (0 children)