Skip to content
Home » File Management in Python

File Management in Python


⭐ File Management in Python

File management refers to reading data from files and writing data into files using Python programs.

Python allows file handling through built-in functions like:

  • open()
  • read()
  • write()
  • close()

It also supports working with different file formats such as:
✔ Text files (.txt)
✔ Binary files (.bin)
✔ CSV files
✔ JSON files

Proper file management enables data storage, record maintenance, logging, and external data processing.


⭐ Why File Handling Is Important?

✔ Data can be stored permanently
✔ Useful for reading configuration files
✔ Helps in data processing and analysis
✔ Used in real applications (banking, billing, inventory systems)


⭐ Basic Steps in File Handling

  1. Open the file
  2. Perform operations (read/write/append)
  3. Close the file

⭐ 1. Opening a File

Use the open() function:

Syntax

file_object = open(filename, mode)

Mode Options

ModeMeaning
"r"Read (default)
"w"Write (overwrites existing file)
"a"Append (adds data to end)
"b"Binary mode
"t"Text mode (default)
"r+"Read + Write
"w+"Write + Read
"a+"Append + Read

Example:

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

⭐ 2. Reading From a File

Python provides multiple ways to read files.

✔ read() — reads entire file

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

✔ readline() — reads one line

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

✔ readlines() — reads all lines as list

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

⭐ 3. Writing to a File

✔ Using write()

f = open("data.txt", "w")
f.write("Hello Python\n")
f.write("File handling is easy.")
f.close()

⚠ "w" overwrites existing file.


✔ Using append mode (“a”)

f = open("data.txt", "a")
f.write("\nThis is added later.")
f.close()

⭐ 4. Closing a File

f.close()

It is important to release system resources and avoid data corruption.


⭐ 5. Using with Statement (Recommended)

The with statement automatically closes the file.

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

✔ No need for f.close()
✔ Safer and cleaner


⭐ 6. File Object Methods

MethodDescription
read()reads entire file
readline()reads single line
readlines()reads all lines
write()writes data
tell()returns current cursor position
seek()move cursor to a specific position
close()closes file

Example:

f = open("data.txt", "r")
print(f.tell())   # cursor position
f.seek(0)         # move to start

⭐ 7. Handling Binary Files

Example:

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

with open("copy.jpg", "wb") as file:
    file.write(data)

⭐ 8. Exception Handling in File Operations

try:
    f = open("abc.txt", "r")
    print(f.read())
except FileNotFoundError:
    print("File not found!")

⭐ 9. Deleting Files

Using os module:

import os

os.remove("data.txt")      # delete file
os.mkdir("newfolder")      # create folder
os.rmdir("newfolder")      # remove folder

⭐ 10. Checking File Exists

import os
print(os.path.exists("data.txt"))

⭐ Exam-Ready Short Summary

File management in Python allows reading and writing data to files using functions like open(), read(), write(), and close(). A file must be opened before operations and closed afterwards. Different modes like r, w, a, and b allow reading, writing, appending, and binary handling. The with statement is recommended as it closes files automatically. File handling is essential for data storage and processing.