Read() mode
In Python, there are many ways to read a file. If we want to extract everything from the file, we can use file.read().
Examples
my_file.txt
This is the first line
This is the second line
This is the third line
Code
sample_file = open("my_file.txt", "r")
print(my_file.read())
sample_file.close()

You can also try this code with Online Python Compiler
Run Code
Output
This is the first line
This is the second line
This is the third line
The close() command frees the system of this application by terminating all resources in use.
Another method of reading a file is to call a certain number of characters, such as in the following code, which will read the first certain number of characters and return a string.
my_file.txt
Hello to the world of programming

You can also try this code with Online Python Compiler
Run Code
Code
sample_file = open("my_file.txt", "r")
print(my_file.read(12))
sample_file.close()

You can also try this code with Online Python Compiler
Run Code
Output
Hello to the world

You can also try this code with Online Python Compiler
Run Code
Write() mode
Write mode is used to write on an existing file, and if the file does not exist, it’ll create the file with that name for us.
Example
Code
sample_file = open('file.txt', 'w')
# if the file does not exist, 'w' will create a new file, else it will overwrite the existing file
sample_file.write("This is the first line\n")
sample_file.write("This line will write after the first line")
sample_file.close()

You can also try this code with Online Python Compiler
Run Code
file.txt
This is the first line
This line will write after the first line
Append() mode
Append mode is used to append on an existing file, and if the file does not exist, it’ll create the file with that name for us.
Example
file.txt
This is the first line
This line will write after the first line
Code
sample_file = open('file.txt', 'a')
# if the file does not exist, 'a' will create a new file, else it will append to the existing file
sample_file.write("\nThis is the third line\n")
sample_file.write("This line will write after the third line")
sample_file.close()

You can also try this code with Online Python Compiler
Run Code
file.txt
This is the first line
This line will write after the first line
This is the third line
This line will write after the third line
Also read, python filename extensions
With open() statement in python
In Python, the with statement is used in exception handling to make the code clearer and easier to comprehend. It makes it easier to handle common resources such as file streams. This is useful since any open files will be closed automatically when finished using this approach, resulting in auto-cleanup.
Examples
Read using with keyword
file.txt
Hello to the world of programming
Code
with open("file.txt", 'r') as fl:
print(fl.read(12))

You can also try this code with Online Python Compiler
Run Code
Output
Hello to the world
Write using with keyword
Code
with open("file.txt",'w') as fl:
fl.write("Write operation using with")
fl.write(" and open")

You can also try this code with Online Python Compiler
Run Code
file.txt
Write operation using with and open

You can also try this code with Online Python Compiler
Run Code
You can practice by yourself with the help of online python compiler.
Must Read Invalid Syntax in Python.
Frequently Asked Questions
-
What is the distinction between the r+ and w+ modes?
If the file does not exist, r+ mode will return an error, but w+ mode will create a new file.
-
What's the distinction between readline() and readlines()?
The distinction between readline() and readlines() is that readline() returns a single line, whereas readlines() returns a list of lines.
-
Each line is terminated by a special character, what is that character in python?
EOL i.e., End of Line, each line is terminated by this character and ‘\n’ is used in python for line breaks
-
What is the distinction between the write and append modes?
Append mode appends new data to the end of the file, whereas write mode overwrites existing data.
Key Takeaways
- Cheers if you reached here! In this blog, we saw and learned File Handling in Python
- We have also seen different modes such as read, write and append
- Further, we saw the with keyword which further reduces our problem of closing the file every time after opening.
On the other hand, learning never ceases, and there is always more to learn. So, keep learning and keep growing, ninjas!
Check out this article - String slicing in Python, Fibonacci Series in Python
Check out the Top 100 SQL Problems to get hands-on experience with frequently asked interview questions and land your dream job.
Good luck with your preparation!