Welcome to CCP
Lets get started !
So, we are reaching towards the end of our beginner's journey and start of our intermediate journey,
Now,
here we're gonna talk about the File objects and I/O operations,
These operations let us open, write and read to a file,
Example:
We have a file called some_text.txt,
we can read it like this:
File to be read:
Python code to read it:
I think it is self-explanatory,
but,
still if you don't understand I'm here to help !
Explanation:
line 1: we declared a variable which is an instance or is a File I/O object and open the file some_text.txt
Line 2: file.read() method reads the whole file and returns so that we can store it in a variable, but here we just wanted to print it
Line 3 - Line 8: the original file that we edited some time ago
Line 9: Notice the extra newline character due to which we got extra newline,
This is because of the print statement,
as the file already had one at its end, the print statement added one also due to which we got an extra newline,
We can resolve this by setting the end argument of print function to ""
Oops ! I forgot to close this file but you should do it by writing...
Ah you guessed it !
by writing: file.close()
Now,
We can also write to files like this:
For writing to files we should one more argument when opening the file is the mode in which we want to open it,
As in this case we want to open it,
Thats why we should pass "w" as the second argument,
we can also provide "r" when opening the file in read mode but we don't do so as the default opening mode is "r" only, you can provide it as second argument but it will be the same,
Now,
Lets our some_text.txt file and ensure that what we have written to it was successful or not:
Hey !
wait a minute,
Where is our previous text that we wrote when this file was made ?
Yes, you are thinking right !
It is deleted, Gone forever !
It happened due to our opening the file with write mode or "w", the moment we opened it in write mode right at the line:
file = open("some_text.txt", "w")
It was deleted,
Suppose if you wanted to append a file via python,
I'm sorry for the loss,
But you remember my friend, in this book of magic (talking about computer languages),
The warnings are always written at the bottom,
We can't resolve the loss but can prevent it from occurring in future,
By of course opening the file in append mode or in "a" mode like this:
Now lets look at our file,
Fingers crossed !
Yesss ! it worked !
Now, I hope you understood this,
Like this you can append as many lines as you want by just separating them by a newline character or \n,
Now,
Lets have look at the iteration of these files and more:
So,
Here,
We can see that,
This loop iterates through a file object line by line,
but,
if we wrote: for x in file.read(),
Then,
It would've iterated through the file.read() string and it would've printed us letters of all the lines on separate lines,
Thus,
For loop iterates through file instances for I/O and file.read() strings differently because file.read() returns string and file returns an iterable file I/O object which are iterated differently,
Hence,
These were the fundamentals of file handling,
Now,
Its your work to find more of these methods and opening modes on python's official documentation and there are indeed more of them,
Best wishes to y'all :)
Lets get started !
So, we are reaching towards the end of our beginner's journey and start of our intermediate journey,
Now,
here we're gonna talk about the File objects and I/O operations,
These operations let us open, write and read to a file,
Example:
We have a file called some_text.txt,
we can read it like this:
File to be read:
Python code to read it:
I think it is self-explanatory,
but,
still if you don't understand I'm here to help !
Explanation:
line 1: we declared a variable which is an instance or is a File I/O object and open the file some_text.txt
Line 2: file.read() method reads the whole file and returns so that we can store it in a variable, but here we just wanted to print it
Line 3 - Line 8: the original file that we edited some time ago
Line 9: Notice the extra newline character due to which we got extra newline,
This is because of the print statement,
as the file already had one at its end, the print statement added one also due to which we got an extra newline,
We can resolve this by setting the end argument of print function to ""
Oops ! I forgot to close this file but you should do it by writing...
Ah you guessed it !
by writing: file.close()
Now,
We can also write to files like this:
For writing to files we should one more argument when opening the file is the mode in which we want to open it,
As in this case we want to open it,
Thats why we should pass "w" as the second argument,
we can also provide "r" when opening the file in read mode but we don't do so as the default opening mode is "r" only, you can provide it as second argument but it will be the same,
Now,
Lets our some_text.txt file and ensure that what we have written to it was successful or not:
Hey !
wait a minute,
Where is our previous text that we wrote when this file was made ?
Yes, you are thinking right !
It is deleted, Gone forever !
It happened due to our opening the file with write mode or "w", the moment we opened it in write mode right at the line:
file = open("some_text.txt", "w")
It was deleted,
Suppose if you wanted to append a file via python,
I'm sorry for the loss,
But you remember my friend, in this book of magic (talking about computer languages),
The warnings are always written at the bottom,
We can't resolve the loss but can prevent it from occurring in future,
By of course opening the file in append mode or in "a" mode like this:
Now lets look at our file,
Fingers crossed !
Yesss ! it worked !
Now, I hope you understood this,
Like this you can append as many lines as you want by just separating them by a newline character or \n,
Now,
Lets have look at the iteration of these files and more:
So,
Here,
We can see that,
This loop iterates through a file object line by line,
but,
if we wrote: for x in file.read(),
Then,
It would've iterated through the file.read() string and it would've printed us letters of all the lines on separate lines,
Thus,
For loop iterates through file instances for I/O and file.read() strings differently because file.read() returns string and file returns an iterable file I/O object which are iterated differently,
Hence,
These were the fundamentals of file handling,
Now,
Its your work to find more of these methods and opening modes on python's official documentation and there are indeed more of them,
Best wishes to y'all :)
Comments
Post a Comment