File Handling in Python

 File handling in Python is used to store data permanently in a file.

Normally, variables store data temporarily in RAM. But when the program ends, data is lost.

To store data permanently, we use files.

Python provides built-in functions to:

  • Create files

  • Read files

  • Write files

  • Append data

  • Delete files

Opening a File

To work with files, first we must open the file using the open() function.

Syntax:

open("filename", "mode")

Example:

file = open("data.txt", "r")

File Modes in Python

ModeDescription
r                            Read mode (default)
w                  Write mode (creates new file or overwrites)
a                  Append mode (adds data at end)
x                  Create new file (error if exists)
rb                  Read binary
wb                 Write binary

Reading a File

There are different methods to read a file:

1. read()

Reads entire file content.

file = open("data.txt", "r")
content = file.read()
print(content)
file.close()

2. readline()

Reads one line at a time.

file = open("data.txt", "r")
print(file.readline())
file.close()

3. readlines()

Reads all lines and returns as a list.

file = open("data.txt", "r")
lines = file.readlines()
print(lines)
file.close()

Writing to a File

w mode is used to write data. It overwrites existing content.

file = open("data.txt", "w")
file.write("Hello Python\n")
file.write("File Handling Example")
file.close()

If file does not exist, it will be created.

Appending to a File

a mode is used to add data at the end of the file.

file = open("data.txt", "a")
file.write("\nNew Line Added")
file.close()

Existing content will not be deleted.

Closing a File

After working with a file, it must be closed using:

file.close()

Closing releases system resources.

Using with Statement

Instead of manually closing the file, we can use with.
It automatically closes the file.

with open("data.txt", "r") as file:
print(file.read())

This is the recommended method.

File Pointer Methods

Python provides two important file pointer functions:

tell()

Returns current file position.

with open("data.txt", "r") as file:
print(file.tell())

seek()

Moves file pointer to specified position.

with open("data.txt", "r") as file:
file.seek(5)
print(file.read())

Working with Binary Files

Binary files are used for images, audio, videos, etc.

Example:

with open("image.jpg", "rb") as file:
data = file.read()
print(data)

Deleting a File

To delete a file, we use the os module.

import os

if os.path.exists("data.txt"):
os.remove("data.txt")
else:
print("File not found")

Example: Student Record Program

# Writing student details
with open("students.txt", "w") as file:
file.write("Name: Arun\n")
file.write("Marks: 85\n")

# Reading student details
with open("students.txt", "r") as file:
print(file.read())

Advantages of File Handling

✔ Stores data permanently
✔ Useful for large applications
✔ Helps in data management
✔ Supports text and binary files

Conclusion

File handling is an important concept in Python.
It allows programs to store, read, update, and manage data from files.

Without file handling, data cannot be stored permanently in applications.

Comments

Popular posts from this blog

Functions in Python

Tkinter -Python

Python – Database Connection