Introduction
Welcome to my 49th blog post on the Python journey. On day 49, I learned about file-handling operations in Python. In this blog, I will discuss open, read, write, append and close functions using file handling in Python. Now Let's dive into more details and understand File handling in Python.
So let's get started......
Opening a File
To manipulate files, python provides several in-built functions. To perform operations on a file, we must first open the file and then do the workings. To open the file we make use of open() function.
open() - This function takes 2 arguments namely - the name of the file and the mode in which the file needs to be opened.
Mode Example -
'r' - reading the file
'w' - writing into the file
'a' - appending into the file
Example -
f = open('myfile.txt', 'r')
By default, the open() function returns a file object that can be used to read from or write to the file, depending on the mode.
Modes in file handling
(r): Read
This opens the file in read-only mode and gives an error if the file does not exist. It is the default mode if no mode is passed as a parameter.
(w): write
Opens the file in write-only mode and creates a new file if the file does not exist.
(a): append
Opens the file for appending only and creates a new file if the file does not exist.
(x): create
This creates a file and gives an error if the file already exists.
(t): text
We need to specify how the file must be handled.
't' mode is used to handle text files. 't' refers to the text mode.
There is no difference between 'r' and 'rt' or 'w' and 'wt' since text mode is the default. The default mode is 'r' (open for reading text, a synonym of 'rt' ).
(b): binary
This mode is used to handle binary files, for example:- images, pdfs, etc
Reading from a File
The read() method reads the entire contents of the file and returns them as a string.
Example -
Here we the contents of myfile.txt and save them in contents as the read methods return an object. Then we print the contents.
f = open('myfile.txt', 'r')
contents = f.read()
print(contents)
Writing to a File
f = open('myfile.txt', 'w')
f.write('Hello, world!')
To write into the file, we first need to open it in write mode and then use the write() method to write into the file.
Appending in a File
Note that writing in a file will overwrite its contents. So we use the append method to avoid overwriting its content. To do this we open the file in append mode.
f = open('myfile.txt', 'a')
f.write('Hello, world!')
Closing a File
It is important to close a file after work is done. This releases the resources used by the file and allows other programs to access it.
To close a file, you can use the close() method.
f = open('myfile.txt', 'r')
# ... do something with the file
f.close()
'with' statement
Alternatively, use the 'with' statement to automatically close the file after you are done with it.
with open('myfile.txt', 'r') as f:
# ... do something with the file
Resources Used
You can watch the video of Day#49 by clicking on the below link ๐๐๐๐๐
Conclusion
Thanks, guys for going through this blog post. On day 49, I learned about many file-handling operations in Python such as read(), write(), open(), append() and close()
Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of Python coding. This is it for Day #49
See you in the next one.....
About Me
Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.
I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.
So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.