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.

Read more about opening in binary mode here:
https://docs.python.org/3/library/functions.html#open

If you enjoy these videos and would like to support my channel, I would greatly appreciate any assistance through my Patreon account:
https://www.patreon.com/coreyms

Or a one-time contribution through PayPal:
https://goo.gl/649HFY

If you would like to see additional ways in which you can support the channel, you can check out my support page:

You can find me on:
My website – http://coreyms.com/
Facebook – https://www.facebook.com/CoreyMSchafer
Twitter – https://twitter.com/CoreyMSchafer
Google Plus – https://plus.google.com/+CoreySchafer44/posts
Tumblr – https://www.tumblr.com/blog/mycms

By |2017-05-01T12:21:05+00:00April 26th, 2017|Programming Video Tutorials, Python Video Tutorials|49 Comments

49 Comments

  1. Behrouz2181 April 26, 2017 at 9:20 pm - Reply

    Your explanations and method is great. Thank you so much for your great tutorials.

  2. Shaikha Al-Hassan April 26, 2017 at 9:23 pm - Reply

    How do you get python to type a word out of other words that has more than three consonants?

  3. Xx[Beat It]xX April 26, 2017 at 9:24 pm - Reply

    can I use "if"? like
    f.read()
    if … :
        …

  4. Amrita Deb April 26, 2017 at 9:27 pm - Reply

    I really admire you, how you can made things so simple.

  5. SHAHIN a April 26, 2017 at 9:29 pm - Reply

    your the bast

  6. abdulsamad abdulrab April 26, 2017 at 9:29 pm - Reply

    Amazing.. am loving Python even more now ! Please dd more tutorials to Files on files like reading from IO, memory, csv files, or extracting data from unstructured format .

  7. Mirek Heikkila April 26, 2017 at 9:30 pm - Reply

    Great Video and amazing explanation! btw <3 the doggy!! 🙂

  8. cameeltje A April 26, 2017 at 9:30 pm - Reply

    please help how do i open locked json files

  9. László Szoboszlai April 26, 2017 at 9:31 pm - Reply

    hi nice vids. got subscribed. but one question: what if you need all the words from the file in a datastructure to do further process. for example you need them in a list, in this case does it matter how you read the file in?

  10. TheJohnny966 April 26, 2017 at 9:33 pm - Reply

    Very comprehensive video!

  11. Sunny Sharma April 26, 2017 at 9:33 pm - Reply

    Great tutorials Corey…Thanks a lot for making all this so simple

  12. xx482 April 26, 2017 at 9:34 pm - Reply

    can you please let me know what seek and tell will do .
    when i use seek in python 3 with mode 0,1,2 there is error thrown . hope i might be wrong in understanding the concept .

  13. Malcolm Powell April 26, 2017 at 9:37 pm - Reply

    Excellent video ….. What is the best approach to read a text file and extracting data from defined colums in the text file ?

  14. miltos hajdini April 26, 2017 at 9:38 pm - Reply

    nice Tutorial, this one helped me a lot in my finals.
    One further question, how did you comment out both three lines at once? which keybinding did you use?
    Thank you.

  15. Anton Demkin April 26, 2017 at 9:40 pm - Reply

    great video, thank you!

  16. Mark Wilson April 26, 2017 at 9:40 pm - Reply

    Oh wow! You rock partner (as your vids).

  17. Eli Eliyahu April 26, 2017 at 9:43 pm - Reply

    Thanl you. It’s perfect!

  18. Madhav Kakumani April 26, 2017 at 9:43 pm - Reply

    Excellent videos. Do you have any videos the shows how to extract the data from a database, transform, group the data and insert into database using python?

  19. debvdo April 26, 2017 at 9:45 pm - Reply

    When you do a type(f) on a file object f obtained by ‘open’, it shows the class as ‘_io.TextIOWrapper’. Why this unusual name? Why does it not show something more obvious like <class ‘File’> ?

  20. Removed By Admin April 26, 2017 at 9:45 pm - Reply

    finally someone that explains it properly! Thank you 🙂

  21. PJ Jones April 26, 2017 at 9:46 pm - Reply

    *** Thanx Corey – Excellent Video. Nicely spoken, great pace, very informative and easily understandable.
    Great work – thank you! :¬)

  22. Kulvir Singh April 26, 2017 at 9:46 pm - Reply

    BEST PYTHON TUTORIALS!!!!!

  23. Андрій Млинко April 26, 2017 at 9:48 pm - Reply

    Your tutorials are the best! So much useful information.
    Thanks a lot!

  24. Y. Singh April 26, 2017 at 9:48 pm - Reply

    Thanks again for your wonderful video !!. What if I wanted to read only the 3rd or 4th line. How it can be done ?

  25. trance peirce April 26, 2017 at 9:49 pm - Reply

    Does anyone know a simple method to read/print specified text? For example, being able to print out the text on line 5, or print out the text between the 100th character in the file and the 200th character in the file, or print out the text between the characters ‘start’ in the file and the characters ‘end’ in the file.

  26. Population Channel April 26, 2017 at 9:52 pm - Reply

    how to read text and save with python, ,,,,,,,,,,,,,,,,,bat file: findstr "text" text_file.txt >>save.txt and how to python??

  27. Left Blank April 26, 2017 at 9:53 pm - Reply

    For reading large files does it make sense to wrap it into a generator or is "f" already an iterator at the point of the context manager?

  28. Ashutosh Gedam April 26, 2017 at 9:55 pm - Reply

    please post some video on how to read and write csv files

  29. tenminutetokyo April 26, 2017 at 9:56 pm - Reply

    DOOOOOOOOOD………….

  30. Emmanuel April 26, 2017 at 9:57 pm - Reply

    hey, thanks for the video.

    How would i create text files in multiple directories, lets say 5 folders.

  31. Mingshuo Ji April 26, 2017 at 9:59 pm - Reply

    Clear and precise instructions!

  32. Lachlan McLeod April 26, 2017 at 10:00 pm - Reply

    Hey Corey. Excellent work! Do you have any idea how to perform a calculation to multiple csv files at once and then export these as new csv files to a new folder location renaming them automatically. For instance, lets say you have 100 csv files located in multiple subfolders and they all have Datetime as their first column, is there a way to automatically resample all csv files to hourly data and then export all these files to a new folder and automatically rename them to include "hour" in their name? Any advise would be much appreciated. Thanks mate.

  33. Agustin Serradell April 26, 2017 at 10:01 pm - Reply

    You genius.
    tutorial = excellent

  34. Y. Singh April 26, 2017 at 10:04 pm - Reply

    PLEASE POST SOME TUTORIAL ON NETWORK PROGRAMMING USING PYTHON….THANKS IN ADVANCE !!

  35. The Soullless April 26, 2017 at 10:07 pm - Reply

    Thank you for creating this, but can you do the webbrowser module?

  36. Maddipati Kesavulu April 26, 2017 at 10:07 pm - Reply

    Good Explanation .

  37. malay ray April 26, 2017 at 10:07 pm - Reply

    hi i am using python 3.5.2 and trying to open csv or text file but getting error in the line… file not available or errorno22. would you help me please

  38. D Shvan April 26, 2017 at 10:07 pm - Reply

    Wondered if you could help me.. How could I add line numbers to the output from reading a text file. Assume I have a text file with 5 random numbers. for example:
    Line 1: 23
    Line 2: 28
    Line 3:….
    and so on… Hope you guys can help 🙂

    #Heres my code. It works, except I want line numbers to the output 🙂

    def main():
    Read_Numbers = open(‘Random_File_Writer.txt’, ‘r’)
    total = 0
    count = 0

    print("Here are the numbers: ")

    for line in Read_Numbers.readlines():
    print(line)

    total = total+int(line)

    count +=1

    print("The total sum of the numbers is: "+str(total))
    print()
    print("The total of numbers read, are : "+str(count))

    main()
    Show less

  39. Hamid Abdanan April 26, 2017 at 10:09 pm - Reply

    I like how organized you are and cover so much. Thank you.

  40. Amrita Deb April 26, 2017 at 10:09 pm - Reply

    Hello great teacher..please still need the tutorial as promised in this video.

  41. SavagePotato April 26, 2017 at 10:10 pm - Reply

    Hey! Awesome tutorial! I have recently been getting really into python and I have made an RPG. In my game, I want to make a save feature which will make a new .py file which you can load from when you re-open the main game, but I haven’t had any luck, and cant find exactly what I am looking for. Any help will be much appreciated, and thank you in advance 🙂

  42. Curt Turdle April 26, 2017 at 10:11 pm - Reply

    i cant believe AJ Styles is teaching a python course 😉

  43. Osshe Louis April 26, 2017 at 10:11 pm - Reply

    What python IDE you use in this video?

  44. Shree April 26, 2017 at 10:12 pm - Reply

    Thanks a lot Corey for your contribution in my python study. somehow I am not able to watch videos from my android device. The audio continues but video pauses every time and surprisingly it is only for your channel (which I love most to follow) 🙁 Please see if you can control that from your end. I know its less likely problem from your end. But not sure why only this channel’s all videos are pausing.

  45. Darth Vador April 26, 2017 at 10:12 pm - Reply

    What’s your python…looker…thing. How do you make python looks like that rather than the default python IDLE.

  46. -MCR - April 26, 2017 at 10:17 pm - Reply

    Anyone Please Reply Fast.
    I write my Python code in Anaconda, and for my report of projects I need to copy these codes to a word file (in which all formatting, colors, etc are all gone). I want to know any command or program that automatically copies my whole code to a microsoft word document with keeping the formatting, coloring, etc of my code, Thank you.

  47. adit kumar April 26, 2017 at 10:18 pm - Reply

    you are one good teacher

  48. PaulGrahamJR April 26, 2017 at 10:18 pm - Reply

    Thank You Corey! You are a talented and gifted Tutor. I have watched over 50 python tutorials and yours are the best example of what I have found on Youtube.

  49. NeuroKernel April 26, 2017 at 10:19 pm - Reply

    Excellent video for file handling in Python

Leave A Comment

*