Python Tutorial: File Objects – Reading and Writing to Files



In this Python Tutorial, we will be learning how to read and write to files. You will likely come into contact with file objects at some point while using Python, so knowing how to read and write from them is extremely important. We will learn how to read and write from simple text files, open multiple files at once, and also how to copy image binary files. Let’s get started.

The code from this video can be found at:

Read more about opening in binary mode here:

✅ 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

Nguồn: https://benjaminjcohen.com/

Xem thêm bài viết khác: https://benjaminjcohen.com/cong-nghe/

33 Comments

  1. instead of saving the copied binary into a .jpg file i saved it into a .txt file then i searched google for a binary to jpg converter, uploaded my file there and downloaded my jpg.. did it just for fun..

  2. what is the difference between f.read and f.readlines ?? it is the same output

  3. Can you upload a tutorial about how to convert a folder of images to a parquet file?

  4. Really good tutorial thanks. Am I right in assuming line is a keyword for file objects?

  5. Outstanding video. Is there a Python book that uses your coding style? I looked at your list of books and didn't see coding style like yours. Thanks

  6. This chanel is something that i will watch all the python tutorials of, and will come back from time to time to refresh what i've learned, thank you.

  7. You are better than the professor from "La casa de papel"!!!!!
    I would marry you :)))
    Seriously,thank you very much!We appreciate you!!!!!!!

  8. this is the content i have been searching for a month now. damn

  9. Does the file have to have .txt after to open it

  10. Absolute killer tutorial! Thank you! I've subscribed. I'm in Lambda School learning python and this cleared up a lot. I appreciate it.

  11. —————————————————————————

    UnsupportedOperation Traceback (most recent call last)

    <ipython-input-59-8e3adc193e9d> in <module>

    2 with open('UD_copy2.jpg','wb') as rf:

    3 chunk_size=4895

    —-> 4 rf_chunk=rf.read(chunk_size)

    5 while(len(rf_chunk)>0):

    6 wf.write(rf_chunk)

    UnsupportedOperation: read

    For the last part of this video where chunk_size was implemented I am getting this error. How can I solve it man?

  12. whats the IDE you are using ?

  13. u r a fucking genius
    keep it up bro

  14. hi mate, how to write two arguments to a file?

  15. is ther any way of searching through the file till it finds the input and get the line?

  16. Yours are some of the best python tutorials I've found on YouTube. No bs. Very well explained. Cheers mate

  17. respected sir you are a legend one of the finest tutorial til date i wish i had a teacher like you to guide me on coding thank you sir for your help

  18. What if we read only half of the binary code and write it in other file?(in case of picture)

  19. I don't get why I would need to chunk read – write when it can do all of it in one go?

  20. I find it a little weird that you talked about read before write. Great video nonetheless.

  21. @ Corey Schafer
    There was a problem with the last code. It did not stop copying chunks
    with open('pic1.png','rb') as rf:
    with open('pic1_copy.png','wb') as wf:
    chunk_size=4096
    rf_chunk=rf.read(chunk_size)
    while len(rf_chunk)>0:
    wf.write(rf_chunk)
    rf_chunck=rf.read(chunk_size)

  22. Brilliant, A good teacher can make you remember concepts with ease. I am delighted to gain from your teachings. Thanks a ton.

  23. 21:30 one of the reasons why liner algebra is useful in cs

  24. how would you write time to a file?

  25. The way this videos are systematically put together makes everything so easy to understand.
    Watching your videos is so helpful and motivating .
    Thank you Corey!

  26. amaaaaaaaaaaaaaaaaaaaaazing

  27. Simply the BEST python tutor ever…truly life saving! And the picture of your dog just made my day 100% better!
    Huge thanks for all!!

  28. how do i get the os's built-in file selector to pop up and just access the name of the file?

Leave a Reply

Your email address will not be published. Required fields are marked *