File Handling in Python With Examples

In this tutorial, we will learn file handling in Python, a fundamental concept that allows users to interact with files by reading, writing, and ext… managing them.

operation in file handling

 

Opening a File: Files can be opened in different modes, such as read (r), write(w), append(a), and. more.

 

  • Let’s see the step-by-step operation on read:
    step1: Create two files one is a Python file the other one is a text file
    step2: in the text file ADD some content
    step3: open a Python file and start doing the operation
    step4: Read operation read()

Let’s see the example code:

file_name=open("text.txt", mode="r") 
data=(file_name.read())    
print(data)

 

Output:

Screenshot-2025-01-06-104818

 

   

  • Let’s see the step-by-step operation on write:

Step 1: Create two files—one is a Python file, and the other one is a text file.
Step 2: In the Python file, use the write operation to add content to the text file.
Step 3: Open the text file in write mode using Python.
Step 4: Write the desired content into the text file using the write() method.

          Let’s see the example code:  

file_name=open("text.txt", mode="w") 
data=(file_name.write("write some content here"))    
print(data)


Output:

text

 

 

 

  • Let’s see the step-by-step operation on append:

Step 1: Create two files—one is a Python file, and the other one is a text file.
Step 2: In the text file, add some initial content.
Step 3: Open the text file in append mode using Python.
Step 4: Append additional content to the text file using the write() method.

 

 Let’s see the example code:  

file_name=open("text.txt", mode="a") 
data=(file_name.write("with this method add more content"))

 Output:

 

  •     Let’s see the step-by-step operation with Read and Write:

   Step 1: Create two files—one is a Python file, and the other one is a text file.
   Step 2: In the text file, add some initial content.
  Step 3: Open the text file in read-and-write mode using Python.
  Step 4: Read the existing content using the read() method and add new content using the write() method.

   Key Points:

  • After reading, the file pointer will be at the end of the file. To write from the beginning, we use seek(0).
  • The write() method returns the number of characters written to the file, which is printed after operating.

Let’s

write-read

see the example code:  

file_name=open("text.txt", mode="r+")  
read_data=file_name.read() 
file_name.seek(0)
print(read_data) 
print(file_name.write("write some content"))

Output:

Read file:

readfile

Write file:

 

write

 

Let’s see the step-by-step operation with write and Read:

   Step 1: Create two files—one is a Python file, and the other one is a text file.
     Step 2: In the text file, add some initial content.
    Step 3: Open the text file in write-and-read mode using Python.
    Step 4: Read the existing content using the write() method and add new content using the read() method.

 

Let’s see the example code:   

file_name=open("text.txt", mode="w+")  
read_data=file_name.write("pyhon")  
file_name.seek(0) 
print(file_name.read())

Output:

write-read

 

 

 

 

 

 

 

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top