Python Tutorial: File Objects – Reading and Writing to Files

///Python Tutorial: File Objects – Reading and Writing to Files

Python Tutorial: File Objects – Reading and Writing to Files

FavoriteLoadingAdd to favorites

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

source

By |2019-06-23T19:59:48+00:00June 23rd, 2019|Python Video Tutorials|24 Comments

24 Comments

  1. Rahul Thaker June 23, 2019 at 7:59 pm - Reply

    for line in f:

    how does it take in one line like we havent specified anything
    line why doesnt line iterate through each character .
    how does it iterate through each line
    can someone please help me? 🙂

  2. Rohan Sirohia June 23, 2019 at 7:59 pm - Reply

    I HAVE A DOUBT. Suppose i want to extract two lines at a time from the test.txt that you have made. Instead of using readline(), how would i know how much bytes in parameters to put in the readline(<parameters>)?

  3. s baskaravishnu June 23, 2019 at 7:59 pm - Reply

    your videos are excellent. Your videos are very much useful to me, Many thanks for that. My warm regards to you.

  4. TechFreak51 June 23, 2019 at 7:59 pm - Reply

    AWESOME !!!

  5. Prateek Yadav June 23, 2019 at 7:59 pm - Reply

    "…it didn't delete the rest of the content" HAHA

  6. Nimer off June 23, 2019 at 7:59 pm - Reply

    Hey bro I need your help, I need to write a code which reads the file and sees if there is enough space is the file for the input to be registered, or one which lets say limits only 20 inputs in a file

  7. Roman Kulak June 23, 2019 at 7:59 pm - Reply

    Thanks a lot for providing good examples!

  8. b k7 June 23, 2019 at 7:59 pm - Reply

    What if the file to read from or write into is in a different directory?

  9. minh quý lê June 23, 2019 at 7:59 pm - Reply

    how can run the python code in ur IDE???

  10. Mr. Quantum June 23, 2019 at 7:59 pm - Reply

    with open('Datenbank.txt', 'r') as f:
    size_read = 10
    f_contents = f.read(size_read)

    print(f.tell())

    But my output is 11 not 10 why?

  11. Gabriel Medina June 23, 2019 at 7:59 pm - Reply

    Thanks COREY, very goog video, I JOIN to your channel. and suscribed too, of course

  12. Ashutosh Parekh June 23, 2019 at 7:59 pm - Reply

    Q1. Is it possible to read from the file while it is opened in "w" mode ? or does it need to be closed and reopen it in "r" mode to read

  13. Kerolos Adel June 23, 2019 at 7:59 pm - Reply

    Thanks so much for your efforts

  14. Ronaldo Laureano June 23, 2019 at 7:59 pm - Reply

    Is there a way to create a file in reading mode?? I cannot :c

  15. Genius by Design June 23, 2019 at 7:59 pm - Reply

    How do I set up this ?

  16. mohammad ahmadzai June 23, 2019 at 7:59 pm - Reply

    very well explained, thank you!

  17. himanshu karki June 23, 2019 at 7:59 pm - Reply

    Thank you very much sir for putting so much effort in helping other by sharing your knowledge.

  18. Debajyoti Majumder June 23, 2019 at 7:59 pm - Reply

    2 years Later: It's still relevant God damn it !! Thanks Corey

  19. AK -16 June 23, 2019 at 7:59 pm - Reply

    Thanks for this tutorial, very practical, explaining how read() can cause a memory bottleneck if the file is huge. One question though … unable to understand at 12:25 how while loops executes its last iteration and exits. From the output its clear that only 4 characters remain to be read at the start of last iteration which is still greater than 0. Can someone elaborate?

  20. Daniel June 23, 2019 at 7:59 pm - Reply

    71 people can't read nor write

  21. Muhammad Ahmed June 23, 2019 at 7:59 pm - Reply

    why is my python white?

  22. EpicPants June 23, 2019 at 7:59 pm - Reply

    Thats a cute fucking dog I love him

  23. Chaya Muppineti June 23, 2019 at 7:59 pm - Reply

    Which text editor you are using and which theme?

  24. Mustafa Ali Saba June 23, 2019 at 7:59 pm - Reply

    What is the difference between doing this:

    with open('graphs/octaedro.txt','rb') as f:
    G = nx.read_edgelist(f,nodetype=int,data=(('weight',int),))
    return G

    and returning G out of with statement?

Leave A Comment

*