How to opeN, read, and then close a file in python

In our daily life, we all deal with different type of files and editing in it by open, reading and then closing it. Sometimes, its very different to search the file to be edited to open from thousand of millions of files in one directory or folder. Therefore, we use python programming to make our life easier for this. We can write, create or close the files using our python code.

There are different modes of opening the files. These modes are “r” for Read-only, “r+” for Read and Write, “w” to write in the file , “w+” to write and read the file, “a” to append the file and “a+” to append and read the file.

There are different method that we can use to open, read or close the file, some of them are:

  • Open the file using open() command in python
  • Read the file using read() command in python
  • Close the file using close() command in python

If you want to learn more about Python Programming, visit Python Programming Tutorials.

Open the file using open() command in python

We can open any file in python using the open() command as shown below:

# open the file using open() function
file = open("My_file.txt")

If we want to append anything in the existing data then we use “a” with the file name (we can use “w, w+, r, r+” to perform different operations according to our desire ). And we use write() command to write anything to append in the file.

# open the file using open() function
file = open("My_file.txt", "a")

# Add content in the file
file.write(" This text will be appended in the My_file")

Read the file using read() command in python

We use read() command to read the data from the file. Such as:

# open the file using open() function
file = open("My_file.txt")

# Reading from file
print(file.read())
My_file.txt

Hello World

If we want to append and read that data,

# open the file using open() function
file = open("My_file.txt", "a")

# Add content in the file
file.write(" This text will be appended in the My_file")

# Reading from file
print(file.read())
My_file.txt

Hello World 
This text will be appended in the My_file

If we want to overwrite something we use, “w” in place of “a”.

# open the file using open() function
file = open("My_file.txt", "w")

# Add content in the file
file.write(" This text will be overwriting the previous in the My_file")

# Reading from file
print(file.read())
My_file.txt

This text will be overwriting the previous in the My_file

Close the file using close() command in python

The following code will open the file and after reading it will close the file using close() command

# open the file using open() function
file = open("My_file.txt")
   
# Reading from file
print(file.read())
 
# closing the file
file.close()

If we want to write something after closing the file, an error will appear and we cannot write after closing the file.

# open the file using open() function
file = open("My_file.txt")
   
# Reading from file
print(file.read())
 
# closing the file
file.close()
 
# Attempt to write in the file
file.write(" Attempt to write on a closed file !")
ValueError: I/O operation on closed file.

Leave a Comment

Your email address will not be published.