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://madisonrep.org/

27 Comments

Jose Jimenez

February 26, 2020

This is great! Thank you. I just have a question on how to take care of a problem I am having with an output text file. I have a program that keeps track of mili-seconds between activity. The output is put into a text file. Sometimes there will be a 0 in the text file. I want to be able to go through the list and find the 0 when it does I want it do find the mean between the number before and after the 0 and input the number inlace of the 0. any idea where I can go to find more information?

Reply

Khondaker Ahmed

February 26, 2020

I am taking DAT-119 Python Programming at CCAC right now and this video has helped me tremendously! Thank you, my dude!

Reply

Laurence B

February 26, 2020

I always every video learn something new!

Reply

Elena Chen

February 26, 2020

Love the turtorials and your dog ! So cute !!!

Reply

lukes

February 26, 2020

hi um i am trying to save game data but how do I do this with a txt file?
if f_contents=="line":

print("text")

it does not do anything can someone help thank you

Reply

Akash Mishra

February 26, 2020

What IDE are you coding it in ?
<3

Reply

Ebenezer Appiah

February 26, 2020

HI Corey are you having an ebook or pdf that we can download

Reply

Felipe André

February 26, 2020

THANK YOU SO MUCH, YOU'RE THE GREATEST!

Reply

Felipe André

February 26, 2020

PYTHON GOD!

Reply

Y I

February 26, 2020

Awesome tutorial Corey! really enjoyed that (thumbs up).

Reply

Andrei-Mihai Chircea

February 26, 2020

This lil nigga ain't trippin over no files mah. Let ya nuts hang, great vids yo!

Reply

anil HDxd

February 26, 2020

read yapıo :d acmıo sadece bunu anladm glb

Reply

Esscapade

February 26, 2020

Can you read say a line and then treat it as a list ? i.e. each word/number read is treated as an element of the list ?

Reply

dillip kumar

February 26, 2020

thanks a ton- simple and easy way you expressed

Reply

Miguel Domingos

February 26, 2020

Why do you use sublime text instead of pycharm?

Reply

Anthony Reeb

February 26, 2020

Awesome video. Anyone know the environment he’s using here?

Reply

epiK

February 26, 2020

What if i wanted to save like test.txt(1), test.txt(2)…. and so on.
how would i do that?

Reply

miillan

February 26, 2020

How can i make it print a random line of my .txt file?

Reply

Malik Rashid Ahmad

February 26, 2020

Dear Corey, Your videos are really very informative. Thanks for this. I have 10 files text files with some information in lines. I want to search some specific words in lines (line by line) and want to write these specific words in line in columns in 1 output text file using Python. Please help me and reply me as soon as possible. my email address is [email protected]
I will be highly thankful to you. Waiting …….

Reply

Caio Consoli

February 26, 2020

This is so fkng good, tkx for the content, srly, well explained and covers a lot of ground, loved it.

Reply

killua zoldyck

February 26, 2020

why didnt he show us the actual file im 9 min in and still dont know what is it reading

Reply

Eli Frank

February 26, 2020

You re better than my teacher… Idk why I pay for university

Reply

Sk Nurul Islam

February 26, 2020

Hello Sir,
I have a question, please help me with the below code:

with open("newfile.txt", "r") as f:

for i in f:

print(i, end=" ")

In above code, we haven't mentioned "read", "readline" or "readlines" anywhere, so how it is reading the lines and returning(printing) the lines ?

Please help

Reply

ScoobyDuh

February 26, 2020

Are you available for a marriage? Great work!

Reply

Caleb Hille

February 26, 2020

fab explanation, such a great teaching explanation

Reply

TheTruthPenguin

February 26, 2020

f = open('C:UsersnoNameDesktopcookies.txt', 'r')

I get this error:
Fatal Python error: Py_Initialize: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00004e6c (most recent call first):

Process finished with exit code -1073740791 (0xC0000409)

I've been trying to get through this for a while now. Any help?

Reply

Saunak Nandi

February 26, 2020

Wow i got really shocked when you show how to work with the image file . Thanks sir "hats off salute" to you

Reply

Leave a Reply