Python Tutorial: Context Managers – Efficiently Managing Resources

///Python Tutorial: Context Managers – Efficiently Managing Resources

Python Tutorial: Context Managers – Efficiently Managing Resources

FavoriteLoadingAdd to favorites

In this Python Programming Tutorial, we will be learning how to use context managers to properly manage resources. Context Managers are great for when we need to setup or teardown some resources during use. So these can be used for: open and closing files, opening and closing database connections, acquiring and releasing locks, and much much more. Let’s get started…

The code from this video can be found at:

Python Object-Oriented Series:

✅ Support My Channel Through Patreon:

✅ Become a Channel Member:

✅ One-Time Contribution Through PayPal:

✅ Cryptocurrency Donations:
Bitcoin Wallet – 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet – 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet – MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey’s Public Amazon Wishlist

✅ Equipment I Use and Books I Recommend:

▶️ You Can Find Me On:
My Website –
My Second Channel –
Facebook –
Twitter –
Instagram –

#Python

source

By |2019-12-03T00:22:08+00:00December 3rd, 2019|Python Video Tutorials|44 Comments

44 Comments

  1. Sompalli NARENDRA December 3, 2019 at 12:22 am - Reply

    Thank you sir your explanation is very nice but you are speaking words is very speed

  2. Jacob December 3, 2019 at 12:22 am - Reply

    you're the man

  3. Tom Hudgins December 3, 2019 at 12:22 am - Reply

    Thanks for the video – well done. One thing I'm always unclear on with Context Managers is how to "stay in the context" over an entire program. In other words, if I want to open a file – say for logging (I know there is a logging module…) – and keep it open for the duration of the program, it seems like all my code has to be within the context manager block (or called from within the block). That seems hard to manage. What's the best way to handle this kind of thing.

  4. Crypto Rootz December 3, 2019 at 12:22 am - Reply

    sorry man , they way you explained this is hella boring and confusing…..

  5. Frank McGuire December 3, 2019 at 12:22 am - Reply

    This was great … I think I can use a generator in a project I am currently working on. I’m trying to mimic a library I am familiar with in another language that links events with commands through an XML file. I’ll put the project on GitHub for you and your community to review and get some feedback in the next few days. Thanks!

  6. Ednilson Alves Lomazi December 3, 2019 at 12:22 am - Reply

    Really nice video. Explains a lot. Thanks.

  7. John Bennett December 3, 2019 at 12:22 am - Reply

    While I can see cases where the try-except code would be important, in this case it is useless or broken. If the first change directory fails, you will still be in the correct dorectory, so no need to change back. If the get directory fails, then the change directory in the finally clause will fail causing an extra error masking the actual problem.

  8. Julio Ribeiro December 3, 2019 at 12:22 am - Reply

    Corey, this video is amazing. You're a very good instructor and your contents are really useful and very easy to understand. Thank you for all videos you've been posting.

  9. Helgurd Mirza December 3, 2019 at 12:22 am - Reply

    How come not creating instance object out from the class?

  10. A E December 3, 2019 at 12:22 am - Reply

    Corey, i enjoyed every second of this video, simple and straightforward.
    Hope to see a video about coroutines as well.

    Thanks.

  11. Shraddha Patil December 3, 2019 at 12:22 am - Reply

    Can you please share application and request context videos?

  12. Dong December 3, 2019 at 12:22 am - Reply

    Phenomenal

  13. Steven Tse December 3, 2019 at 12:22 am - Reply

    thankyou, and indeed Corey is the best Python teacher on the internet. if I may, can you teach us on flutter with Python, ethical hacking, python on raspi…? thankyou!!

  14. Ilan Aizelman December 3, 2019 at 12:22 am - Reply

    Gold!!!!!! Thank you.

  15. anurag trivedi December 3, 2019 at 12:22 am - Reply

    As usual, excellent video Corey !

  16. Donald Martin December 3, 2019 at 12:22 am - Reply

    I have a white belt in Python and this really helps. Thanks

  17. Donald Martin December 3, 2019 at 12:22 am - Reply

    Which software package do you use during your presentation? I'm not happy with mine. Too slow.

  18. Diego Ugarte December 3, 2019 at 12:22 am - Reply

    You are simply the best!

  19. Manivannan Pandi December 3, 2019 at 12:22 am - Reply

    f.closed is an attribute of the object??

  20. Prabhath Kota December 3, 2019 at 12:22 am - Reply

    Explained beautifully

  21. Joe Tang December 3, 2019 at 12:22 am - Reply

    Corey is the Python King!

  22. Piotr Wln December 3, 2019 at 12:22 am - Reply

    Congrats on having 200 000 subscribers now!

  23. Sean O'Connor December 3, 2019 at 12:22 am - Reply

    Well done.

  24. Ernest G. Wilson II December 3, 2019 at 12:22 am - Reply

    @CoreySchafer after several months of watching videos on the internet and going through various online training including ones I purchased, I can safely say this channel and specifically Corey Schafer has the best Python training videos on YouTube and perhaps on the entire internet! Thank you very much for taking the time to make these videos and share them with all of us! I find myself repeatedly revisiting your videos as an excellent training reference, well done! I am of course subscribed with notifications turned on and thumbs up! I wish I could give this channel a million thumbs up! I let all the videos play to the end on both my browser and on Chromecast and I hope that boosts your statistics and views, you certainly deserve credit for excellent training and making the Internet a better place for us all! I will be joining your Patreon and helping to support this channel, something I do not often do with limited funding and only reserve the very best content which this certainly is!

  25. Corine Lamagdeleine December 3, 2019 at 12:22 am - Reply

    Hi Corey, Very good video to unterstand Python contextmanager.

  26. Colver December 3, 2019 at 12:22 am - Reply

    great tutorial!!!

  27. norindeth mao December 3, 2019 at 12:22 am - Reply

    I am pretty thanks for this video and your videos. No doubt, just I need to try myself. And I would like to show the sending without and with attachment files in Python. Hope you are doing fine.

  28. borislav hadzhiev December 3, 2019 at 12:22 am - Reply

    hey Corey, just a random suggestion if you run out of ideas for videos. I don't think any1 has covered unicode in Python, and the whole concept of encoding unicode code points into bytes and then decoding the bytes using the correct encoding back into unicode. For example when I first watched https://nedbatchelder.com/text/unipain.html lots of things made sense.

  29. Suvodeep Roy December 3, 2019 at 12:22 am - Reply

    So nice explanation i had never seen before. Please make a series of C programming for us.

  30. Mike Burt December 3, 2019 at 12:22 am - Reply

    Corey, thank you for posting these tuts on Python. I've found them all so interesting. I really enjoyed understanding how context managers work and thanks to your video on them I've already made use of one in a project. Thanks again, buddy. They're much appreciated.

  31. vinay kumar December 3, 2019 at 12:22 am - Reply

    You are Awesome

  32. Zekun Zhang December 3, 2019 at 12:22 am - Reply

    this is crazy man so good

  33. Hitesh Chalise December 3, 2019 at 12:22 am - Reply

    really good video

  34. Xin Zhao December 3, 2019 at 12:22 am - Reply

    What if try open with error? Then f is a None, it has no close method' right?

  35. Raghav Gupta December 3, 2019 at 12:22 am - Reply

    just wow!

  36. Shafiq ur Rahman December 3, 2019 at 12:22 am - Reply

    Love the examples you gave. Very practical!

  37. satya behera December 3, 2019 at 12:22 am - Reply

    nice video…

  38. mohamed sleymi December 3, 2019 at 12:22 am - Reply

    thank you very much , you are awesome

  39. El Jefe December 3, 2019 at 12:22 am - Reply

    Hey Corey. Excellent video. Your channel is the best on YT in my humble opinion. Can you create a series of videos concerning data structures and algorithms for technical interviews? I think you’d be awesome at it since your teaching ability is 10 out of 10 😄

  40. Kishore Kumar December 3, 2019 at 12:22 am - Reply

    Hi Corey could you please teach map,filter and reduce functions

  41. Lowzenza December 3, 2019 at 12:22 am - Reply

    Hi Corey, do you have videos or could you make a playlist on how to use the python package pandas?

  42. Arsenal Karim December 3, 2019 at 12:22 am - Reply

    Still waiting for your video everyday (especially django) 🙁

  43. Abdemanaaf Ghadiali December 3, 2019 at 12:22 am - Reply

    Hi, I was just hoping would you start python networking tutorials or direct me to some good video tutorials for it.. It might help me a lot… Thank You!!!

  44. Kevin L December 3, 2019 at 12:22 am - Reply

    Still the best python teacher on the web

Leave A Comment

*