File IO in Python | Python Tutorial - Day #49

File IO in Python | Python Tutorial - Day #49


File IO in Python | Python Tutorial - Day #49

Learn how to read and write files in Python with this easy-to-follow tutorial. Discover the different methods for handling file input and output, as well as how to work with different file formats. This video is part of our Python tutorial series and is perfect for beginners. Don’t miss out on Day #49 of our Python journey!
Python is one of the most demanded programming languages in the job market. Surprisingly, it is equally easy to learn and master Python. This python tutorial for absolute beginners in Hindi series will focus on teaching you python concepts from the ground up.
Access the Playlist:    • Python for Beginners (Full Course) | …  
Link to the Repl: https://replit.com/@codewithharry/49-
Join Replit the browser-based IDE used in this course - https://join.replit.com/code-with-har
►Checkout my English channel here:    / @programmingwithharry  
►Instagram: www.instagram.com/codewithharry

python, C, C++, Java, JavaScript and Other Cheetsheets [++]:
Playlist:    • Coding CheatSheets 🧾 by CodeWithHarry  

►Learn in One Video[++]:
Python[15 Hr]:    • Python Tutorial For Beginners In Hind…  
Python Advance[3.5 Hr]:    • Python Programming Course in Hindi (A…  
Python[1 Hr]:    • Learn Python In Hindi In One Video - …  
Python[2 Hr]:    • Python Tutorial In Hindi 🔥  
Python[15 Min]:   • 15 Minute Python Tutorial For Beginne…  
JavaScript[1 Hr]:    • JavaScript Tutorial  
C[1.3 Hr]-   • C Programming Tutorial For Beginners:…  
php[1 Hr]:    • Learn Php In One Video In Hindi - हिं…  
php[2.3 Hr]:   • Php Tutorial for Beginners in Hindi w…  
php[Project]-    • Login And Registration Form Using Php…  
HTML[30 Min]:   • HTML 5 Tutorial For Beginners In Hind…  
CSS[8.5 Hr]:   • CSS Tutorial In Hindi (With Notes) 🔥  
CSS[1.4 Hr]:   • CSS 3 Tutorial For Beginners: Learn C…  
Wordpress[3.2 Hr]:   • How To Make a WordPress Website | Wor…  
Angular[2 Hr]:   • Angular Tutorial in Hindi  
Java[2.3 Hr]:   • Java tutorial in hindi 🔥  
Web Scraping[1 Hr]:   • Web Scraping Tutorial using Python an…  
MongoDB[2 Hr]:   • MongoDb Tutorial For Beginners in Hin…  
JQuery[1.1 Hr]:   • jQuery Tutorial For Beginners In Hind…  
Git and GitHub[1.1 Hr]:   • Git \u0026 GitHub Tutorial For Beginners I…  

►Complete course [playlist]:
React:    • React Js Tutorials in Hindi  
Python-   • Python Tutorials For Absolute Beginne…  
OOP Python-   • Object Oriented Programming Tutorials…  
Java:   • Java Tutorials For Beginners In Hindi  
JavaScript-    • JavaScript Tutorials In Hindi  
PHP-   • PHP Tutorials in Hindi  
C-   • C Language Tutorials In Hindi  
C++-   • C++ Tutorials In Hindi  
Git \u0026 GitHub-   • Git and GitHub Tutorials In Hindi  
Android Dev-    • Android Development Tutorials in Hindi  
Python GUI-    • Python GUI: Tkinter Tutorial In Hindi…  
Web Development-    • Web Development Tutorials For Beginne…  
Python Django:   • Python Django Tutorials In Hindi  
Projects Using HTML, CSS \u0026 Javascript-    • Projects Using HTML, CSS \u0026 JavaScript…  
Data Structure and Algo:   • Data Structures and Algorithms Course…  

Follow Me On Social Media
►Website (created using Django Rest \u0026 Angular): https://www.codewithharry.com
►Facebook: https://www.facebook.com/CodeWithHarry
►Instagram: https://www.instagram.com/codewithharry/
Twitter: https://twitter.com/CodeWithHarry
Comment “#HarryBhai” if you read this 😉😉


Content

0 -> So, we have seen a lot of things about Python programming
2.541 -> today I will tell you how to handle a file in Python.
5.747 -> Because 'file handling' is a very important concept in Python
9.312 -> how to open a file,
10.784 -> how to write it,
12.324 -> how to append in that text file
14.333 -> It can be a binary file instead of a text file
17.789 -> in that case how to handle it
19.613 -> I will tell you all this in today's video
21.785 -> Let's go to the computer screen
23.321 -> And let's get started
34.192 -> So, there are methods of file handling inside python
36.353 -> it is built-in
37.306 -> in this repl you see,
38.616 -> I create a python file here first of all
41.462 -> Because it should be very easy for you to understand
44.109 -> then here we will see how any basic file is opened in Python.
49.315 -> When we talk about file
50.438 -> the first thing that comes to our mind is a 'text file'.
53.434 -> Although files can be text files as well as binary files
56.988 -> and text files can contain many types of files
59.756 -> which can contain textual content.
61.839 -> But for now, let us see in a very simple way
65.059 -> that how file handling goes in Python
67.017 -> Python provides several ways to manipulate files.
69.733 -> today we will discuss the same.
71.014 -> you use 'open method' to open a file
75 -> then simply you can say
76.348 -> f = open('myfile.txt')
78.952 -> now this 'myfile.txt' is not present here.
81.445 -> doesn't exist at all
82.439 -> if I run, I will see the error
84.813 -> You see here it is saying
86.049 -> that what is the 'myfile.txt'
87.956 -> how to open the 'myfile.txt'
89.845 -> if there is no file, then how will it open
91.785 -> then the same thing is being spoken by Python.
94.116 -> Now you see here
95.476 -> Python provides the open() function to open a file
97.363 -> It takes two arguments
98.3 -> What is the first argument?
99.407 -> the name of the file
100.425 -> and the second argument is the mode
102.455 -> in which the file should be opened.
105.095 -> The mode can be 'r' for reading,
107.07 -> 'w' for writing,
108.213 -> and 'a' for appending
110.192 -> Means, you can read a file,
112.292 -> write it from 'w'
113.99 -> and append to that file if you want
116.777 -> Means add additional content in that file at the end of the file
120.81 -> you can do that by append mode by writing 'a' here, ok
124.201 -> Let all these things keep happening
125.824 -> now I will tell you one more thing
127.922 -> if I want to open this file
130.292 -> then I can create it, ok
131.525 -> so here I do one thing
132.722 -> create a new file
133.926 -> I'll name it 'myfile.txt'
136.301 -> I'll write the content inside it
139.547 -> Hey harry awesome man!
142.238 -> Ok
142.738 -> will write this
143.595 -> And here I will open 'main.py' again
146.661 -> and then run this program
148.599 -> this time interestingly
150.646 -> this program will not pass through the error
152.163 -> Why won't it error through?
153.378 -> because now I have the 'myfile.txt'
156.397 -> but I just created a type of variable here
158.951 -> And I have said that just open the file
160.875 -> even I didn't print anything
162.439 -> I haven't done anything
163.503 -> Let's do one thing, let's print 'f'
165.452 -> and see what scene is made
166.988 -> so, if I print 'f'
168.549 -> So, see here some object is getting printed
171.051 -> text io or repl
172.356 -> now how can I extract the content of this file
175.234 -> how can I read its content
178.059 -> So, what will i do here
179.161 -> I will say, text = f.read()
182.016 -> and after that f.close()
184.043 -> what have I done that I want to tell you
187.413 -> so, I'll be here quickly do print 'text' instead of printing 'f'
191.171 -> There is no point to print it
193.233 -> I would like to print the textual content of this file
195.926 -> which I have done now
196.768 -> you can see it in your screen
198.005 -> Hey harry awesome man! is coming by writing
200.279 -> this is the same content
201.29 -> which is present inside my 'myfile.txt', ok
205 -> So, you guys saw here
206.282 -> that I extracted the content of this text file
209.707 -> by doing 'f.read'
211.925 -> Will I be able to extract the contents of this file
215.233 -> if I open it in write mode?
216.598 -> Absolutely not
217.871 -> Look here it is saying that you cannot read
220.262 -> you have opened in write mode
221.491 -> you remember that
222.307 -> you said first I am going to write
224.197 -> And after that you sat down to read
225.972 -> it is wrong, ok
227.226 -> so, it said this
228.085 -> so, if you want to read, then use 'r'.
231.361 -> If you want to do write, then use 'w'
233.12 -> And stick to what you say next
236.106 -> It should not happen that you sat down to read the file
238.128 -> after opening it in write mode
239.507 -> or you sat down to write it
240.769 -> after opening it in read mode
242.057 -> No-no sir, it will not work
243.674 -> now I will run it again
245.468 -> so, you guys see here 'Hey harry awesome man! is written, ok
249.156 -> So, it's going perfect over here
252.152 -> now we see the first name, the first argument
255.092 -> that makes sense to me.
256.725 -> You guys will say yes brother harry bhai everything is right
259.377 -> All is well
260.405 -> but what is the story of this second argument
262.271 -> which is the mode
264.506 -> This means that sometimes you are saying 'r',
266.816 -> sometimes you are saying 'w',
267.726 -> sometimes you are saying 'a'
268.414 -> tell a little about it
269.724 -> So let me tell you
270.853 -> read mode opens the file for reading only
272.855 -> and gives the error if the file does not exist
275.284 -> we had just seen that
276.863 -> 'myfile.txt' was not existing
278.965 -> an attempt was made to open it
280.598 -> But the error came
282.033 -> now if you say
283.943 -> that I will open the door of Ferrari
285.409 -> which is parked downstairs in my house
287.463 -> So how will you open the door of something
288.892 -> that does not exist
290.51 -> How will you open? will not open
292.063 -> So, in the same way this file also does not exist, ok
294.872 -> it will exist later as we have created this file
297.526 -> it will exist later
298.896 -> What will happen later
300.171 -> will be known only later
302.031 -> but at the moment it does not exist
303.58 -> so, it will throw an error.
304.791 -> This is the default mode if no mode is passed as a parameter
308.59 -> Now this is very interesting
310.291 -> If I do it like this too
312.09 -> and try to run it
313.724 -> interestingly this program works
315.752 -> Why?
316.372 -> because the 'r' mode is the default
318.349 -> Ok
319.884 -> so, I turn it to Ctrl + g
321.261 -> Because I want to be specify
323.098 -> that I'm opening this in read mode the file, ok
326.376 -> that's great
327.213 -> We have seen the write mode
328.13 -> that if we want to write in any file
330.224 -> then we will open it in write mode
331.732 -> or if we want to create a new file
333.938 -> So, can do it too, ok
335.682 -> so, I over here, if I do 2 over here, 'w', and run this
338.761 -> then 'myfile2' that's here will be built
340.824 -> You guys are seeing that it has become an 'myfile2' here.
342.967 -> Well then you should know this thing
344.712 -> that in write mode if you open a file
347.726 -> which does not exist
349.935 -> it will be created as it is, ok
352.675 -> Awesome
353.593 -> What is append mode?
354.509 -> If you want to append
356.621 -> that is, you want to write to the end of the file
360.48 -> And the content of the file should also be maintained
362.305 -> as if I open the file containing 'Hey harry awesome man!
365.166 -> here in write mode and write it
367.082 -> then this content will be broken.
368.559 -> I don't want this
369.92 -> I want to put my content at the end of it
374.517 -> so, what will I do to do that
376.539 -> I will open this file in append mode, nice
378.974 -> What is create mode?
379.896 -> this is (x) mode
381.15 -> in this mode the file is created
383.245 -> and if the file already exists
385.244 -> then it throws an error, ok
386.829 -> Then after this I talk about (t) and (b)
388.974 -> Apart from these modes,
390.377 -> which we have already seen above
392.326 -> we also specify that this file is being read
396.107 -> like if i do 'rt' which I put later here 't'
399.185 -> this 't' means
400.768 -> that I want to open it as a text file
402.647 -> which is by defaults
403.729 -> I didn't need to put 't'
405.421 -> if I put 'b' in here like this
407.45 -> So, I'm saying open it on a binary basis
410.003 -> This is a binary file
411.56 -> so, this content will come
413.007 -> this binary content will come
414.187 -> you can see here it is coming in the form of bytes, ok
417.078 -> So, I'll use 'rb' whenever I want to open a binary file
419.839 -> Like to open a 'jpg' file,
421.468 -> to open an 'image' file,
422.477 -> or to open a 'pdf', or to open a 'exe' file
425.022 -> I will use 'rb' If I want to read that
427.513 -> Similarly,
428.557 -> here is the way we have read
430.632 -> which I have already told you
431.771 -> We can also write
432.738 -> So I told you about read
433.59 -> once we have a file object,
434.477 -> we can use various method to read from the file
437.604 -> there is a read method
438.614 -> that I have just told you
439.803 -> which gives you the textual content inside that file.
443.282 -> Read lines is also a method
444.481 -> I will tell you about it in the next video
446.198 -> but for now, we are going to talk about how to write in a file
450.924 -> Now suppose I want to write a file
452.749 -> I comment it out
454.173 -> It was reading a file
455.707 -> I do a work here, I write a comment here
458.629 -> 'READING A FILE'
459.883 -> So here I wrote 'reading a file'
461.293 -> I do that like this
462.867 -> now I do 'WRITING TO A FILE' here, ok
464.901 -> a little bit of clarity will remain
466.929 -> 'WRITING TO A FILE'
467.971 -> I close Caps lock
469.47 -> and here you see I have opened a file like this
473.759 -> and after opening
474.563 -> suppose I want to put some textual content in it
476.509 -> I want to say that
478.438 -> I run this first
480.271 -> you see here I want to put 'Hello, world!'
482.654 -> in this file named 'myfile.txt'
485.65 -> In fact, this file was created
486.904 -> I should have put it inside 'myfile2.txt'
489.465 -> but it is too late
490.352 -> the content inside 'myfile.txt' is blown up
492.996 -> So, I see what the problem is here
494.386 -> in my main.py,
495.331 -> when I write it in 'myfile.txt'
497.634 -> then why it didn't write
498.566 -> I wrote 'myfile.txt'
500.517 -> wrote it in 'w' mode
502.177 -> inside it I wrote 'Hello, world!' Tried to write
505.381 -> but 'Hello, world!' did not come
506.951 -> what kind of scene is being here
507.86 -> Why is this program not running
509.657 -> I opened 'myfile.txt' in write mode
512.077 -> Now you see,
512.577 -> keep in mind that writing to a file will overwrite its content
515.619 -> If you want to append to a file
516.879 -> so, you use append mode to it
518.384 -> But what is happening here is
520.097 -> that I am trying to write inside 'myfile.txt'
521.467 -> which is already there
522.911 -> and which is still empty.
526.676 -> And I also wrote here
528.634 -> and after writing you see that
531.296 -> this content did not come in it
533.775 -> now here it is important
534.774 -> that, I'll also close its file as it is by typing 'f.close' here
538.747 -> and now I'll look at 'myfile.txt', so 'Hello, world!' has come
542.286 -> If I open the same file in append mode, ok
544.889 -> And I run it exactly like this
547.015 -> every time I run it will add 'Hello, world!'
549.577 -> Look, once it was also add 'Hello, world!'
551.22 -> if I run it again
553.25 -> then once more 'Hello, world!' will be added
554.68 -> because it's appending
555.786 -> append means appending text to the end of a file, ok
559.128 -> so, this thing is happening
560.276 -> So, hope you have understood this thing, ok
563.481 -> that how to be reading the file,
564.935 -> how to write on the file
567.104 -> you should know all these things
568.871 -> If you want to handle a file
572.116 -> or you want to learn file handling in Python
574.412 -> now there is another interesting method
575.878 -> A lot of people will do argue
577.093 -> in fact, do right argue
578.751 -> that this method is a little boring
580.678 -> that first make an 'f', ok
583.206 -> Then after that what you do is to write 'f.write' like this
588.01 -> and then remember to close
589.666 -> It is necessary to close
591.263 -> because if you have taken out any item from the fridge
593.586 -> whenever you come out from the fridge, some water in the heat
596.744 -> And do you get the water out from there?
599.516 -> I know many people get out by taking out water
601.553 -> then their mother tells them
603.268 -> But the good kids are they close the fridge, ok
606.145 -> so, you close the fridge
607.247 -> then Ice-cream comes out of the fridge
608.081 -> and you close the door of the fridge or leave it open
610.443 -> Does the door keep hanging like this? No!
613.433 -> that's the way we close the file over here
615.376 -> but there's a way, you know what?
617.686 -> with open('myfile.txt', 'a')
622.299 -> I can do exactly as I am doing
626.058 -> and inside this if I write something like this
628.452 -> f.write("Hey I am inside with")
632.71 -> what will happen if I write like this?
635.246 -> Do you know?
636.098 -> So, I don't need to do 'f.close'
638.288 -> our file is automatically closed by using 'with statement'.
642.243 -> Whatever file we want to use it
644.574 -> we can do whatever mode we want to use it here.
648.306 -> Here I am using 'myfile.txt'
650.535 -> I am opening it in append mode
652.008 -> and I am saying 'f.write' inside it write the
655.185 -> "Hey I am inside with"
657.264 -> We all know what 'myfile.txt' looks like at this point
661.05 -> but if I run this program
663.007 -> This program will add a 'Hello, world!'
665.096 -> And after this "I am inside with" this will be added
668.186 -> But what is happening here?
669.657 -> It is saying that, 'I/O operation on closed file'
671.856 -> ok so I do one thing, comment it out
674.457 -> I run this thing at a time
676.104 -> and here it's saying, 'f' is not defined
679.029 -> so, I have to write 'as f' here, ok
681.722 -> Well now I can keep this too
682.917 -> I didn't write 'as f' that was the problem
684.872 -> so, I can do this too
685.729 -> I opened the file once, closed again Opened again
687.951 -> automatically closed because I was inside 'with'
690.584 -> so now you see here 'myfile.txt' gets "Hey I'm inside with"
694.944 -> Ok
695.557 -> I will run this program as many times as It will come
699.495 -> why 'file handling' is important
701.255 -> why you need to know 'file handling'
703.436 -> So, the reason is
705.139 -> that if you know file handling very well
708.776 -> So, suppose you are making any game
710.617 -> you have to save its highest score, you can do that, ok
714.074 -> You can store any data of your program inside the file
717.967 -> in a way you can use the file as a tempt database, ok
724.333 -> You can use it as a data store
727.46 -> If you want to store anything
728.904 -> I want to store marks for 50 students, you can do that too
732.46 -> So that's quite a handy thing, ok
734.497 -> so, I hope you get the points
736.285 -> How to do basic file is done in I/O Python.
738.792 -> In the next video I will tell you about more interesting functions
741.812 -> that are used for file handling
743.741 -> for now, that's all in this video
745.17 -> If you haven't accessed this playlist yet
747.662 -> be sure to access it
749.511 -> Thankyou so much guys for watching this video
751.849 -> And I will see you next time.

Source: https://www.youtube.com/watch?v=eDBPlcWYses