Popular Searches
Popular Course Categories
Popular Courses

Introduction to File Handling in Python

Introduction to File Handling in Python

File Handling and Data Serialization

Introduction to File Handling in Python

Course: Python Training

Topic: Introduction to File Handling in Python

Course Link: Python Training Course at JustAcademy

Demo Link: Register for a Course Demo

Website: Visit JustAcademy


1. Introduction to File Handling

File handling in Python is the process of creating, opening, reading, writing, appending, and managing files through a Python program. It allows applications to store information permanently so that the data remains available even after the program finishes running.

Variables are useful for temporarily storing data in memory, but their values generally do not persist after the program terminates. Files provide a way to save information such as user details, reports, configuration settings, transaction records, application logs, and text documents.

Python provides the built-in open() function and file objects to perform file operations. The with statement is commonly used to manage files safely because it automatically closes the file when the block finishes.

Examples of File Handling in Real Applications

  • Saving student names and marks in a text file.
  • Reading product information from a CSV file.
  • Storing application logs for troubleshooting.
  • Reading configuration settings when an application starts.
  • Writing reports generated by data analysis programs.
  • Loading and saving JSON data for application settings or records.
  • Reading images, audio files, and other binary resources.

2. Learning Objectives

After completing this topic, learners will be able to:

  • Explain the purpose of file handling in Python.
  • Open and close files using Python.
  • Understand file modes such as r, w, a, and x.
  • Read complete files and individual lines.
  • Write new content and append existing content.
  • Use the with statement for safe file management.
  • Handle common file-related exceptions.
  • Understand text files, binary files, file paths, and encodings.
  • Build a simple application that reads and writes files.

3. What Is a File?

A file is a named collection of data stored on a storage device. Files may contain plain text, structured data, images, audio, videos, program code, or other information.

Python interacts with files through a file object returned by open(). This object provides methods for reading, writing, and managing the opened file.

Common File Types

File Type Extension Common Purpose
Plain text .txt Notes, messages, and simple records.
Comma-separated values .csv Tabular data such as student or sales records.
JSON .json Structured data and application configuration.
Python source .py Python program code.
PDF .pdf Documents and reports.
Image .jpg, .png Photographs, diagrams, and graphics.
Binary data Various Images, audio, executable files, and other non-text data.

4. The open() Function

The built-in open() function opens a file and returns a file object. The file object is used to perform operations such as reading or writing.

Syntax

open(file, mode="r", encoding=None)

The function accepts additional parameters, but the most important ones for beginners are:

  • file: The file name or path to the file.
  • mode: Specifies whether the file is opened for reading, writing, appending, or another operation.
  • encoding: Specifies the text encoding when the file is opened in text mode. UTF-8 is a common choice.

Example: Opening a File

file = open("sample.txt", "r", encoding="utf-8")
content = file.read()
print(content)
file.close()

This example opens sample.txt in read mode, reads its contents, prints the content, and closes the file.

Important: The file must already exist when it is opened in read mode. If it does not exist, Python generally raises FileNotFoundError.

5. Understanding File Modes

A file mode determines what operations can be performed and how Python treats the file when it is opened.

Mode Purpose Behavior
r Read text Opens an existing file for reading. Fails if the file does not exist.
w Write text Creates a file if necessary or truncates an existing file to empty it.
a Append text Creates a file if necessary and writes new content at the end.
x Exclusive creation Creates a new file and fails if the file already exists.
r+ Read and write Opens an existing file for both reading and writing without initially truncating it.
w+ Write and read Opens for reading and writing but truncates an existing file.
a+ Append and read Opens for reading and appending; writes are directed to the end.
rb Read binary Reads binary data as bytes.
wb Write binary Writes bytes and truncates an existing file.
ab Append binary Appends bytes to a binary file.
rt Read text Explicit text-reading mode; equivalent to r.

Important distinction: The w mode can erase existing content immediately when the file is opened. Use it carefully when working with important files. Choose a when you want to preserve existing content and add new data at the end.

6. Creating and Writing to a File

The write() method writes a string to a file. When the file is opened in w mode, Python creates the file if it does not exist or replaces its existing content.

Example: Writing Text

with open("student.txt", "w", encoding="utf-8") as file:
    file.write("Name: Rahul\n")
    file.write("Course: Python\n")
    file.write("Status: Enrolled\n")

print("Data written successfully.")

Output:

Data written successfully.

The file student.txt now contains:

Name: Rahul
Course: Python
Status: Enrolled

Understanding the Code

  • open("student.txt", "w", encoding="utf-8") opens the file for writing.
  • with manages the file's lifetime.
  • file.write() writes a string to the file.
  • \n represents a newline character.
  • When the with block ends, Python closes the file automatically.

7. Reading an Entire File with read()

The read() method reads file content and returns it as a string in text mode. If called without a size argument, it attempts to read the remaining content of the file.

Example

with open("student.txt", "r", encoding="utf-8") as file:
    content = file.read()

print(content)

Output:

Name: Rahul
Course: Python
Status: Enrolled

Reading a Limited Amount of Content

You can pass a size argument to read a limited amount of content.

with open("student.txt", "r", encoding="utf-8") as file:
    content = file.read(4)

print(content)

Output:

Name

In text mode, the size generally specifies the maximum number of characters to read, rather than a number of lines.

Best practice: Avoid reading very large files completely into memory when you can process them incrementally.

8. Reading One Line with readline()

The readline() method reads one line from the file. If a newline character is present, it is generally included in the returned string.

Example

with open("student.txt", "r", encoding="utf-8") as file:
    first_line = file.readline()
    second_line = file.readline()

print(first_line, end="")
print(second_line, end="")

Output:

Name: Rahul
Course: Python

Calling readline() repeatedly moves through the file one line at a time. An empty string indicates that the end of the file has been reached.

9. Reading All Lines with readlines()

The readlines() method reads the remaining lines of a file and returns them as a list of strings.

Example

with open("student.txt", "r", encoding="utf-8") as file:
    lines = file.readlines()

print(lines)

Output:

['Name: Rahul\n', 'Course: Python\n', 'Status: Enrolled\n']

Each list element represents a line. The newline character is usually included when the original line ends with one.

When to use it: Use readlines() when you specifically need the file's lines as a list and the file is reasonably small. For large files, iterate over the file object to avoid storing every line in memory at once.

10. Reading a File Using a for Loop

Iterating directly over a file object is a simple and memory-efficient way to process text line by line.

Example

with open("student.txt", "r", encoding="utf-8") as file:
    for line in file:
        print(line, end="")

Output:

Name: Rahul
Course: Python
Status: Enrolled

This method is especially useful for processing large log files, reports, and datasets one line at a time.

11. Appending Data with append Mode

The a mode opens a file for appending. If the file exists, new content is added at the end without removing existing content. If it does not exist, Python creates it.

Example

with open("student.txt", "a", encoding="utf-8") as file:
    file.write("Location: Mumbai\n")

print("New information appended.")

Updated file content:

Name: Rahul
Course: Python
Status: Enrolled
Location: Mumbai

Appending is useful for maintaining logs, recording new transactions, and adding records over time.

Tip: The append mode positions writes at the end, but it does not automatically insert a newline before new content. Add \n when required by the file format.

12. Using the with Statement

The with statement is the recommended approach for opening files in most situations. It uses Python's context management system to ensure that the file is closed when the block finishes, even if an exception occurs.

Recommended Approach

with open("notes.txt", "r", encoding="utf-8") as file:
    content = file.read()
    print(content)

Alternative Without with

file = open("notes.txt", "r", encoding="utf-8")

try:
    content = file.read()
    print(content)
finally:
    file.close()

Both approaches can close the file safely. However, with is shorter and easier to maintain.

Advantages of with

  • Automatically closes the file when the block ends.
  • Helps prevent resource leaks.
  • Works correctly when an exception occurs inside the block.
  • Makes file-handling code easier to read.
  • Reduces the need for manual cleanup logic.

13. Closing a File with close()

The close() method closes a file object and releases resources associated with it. When you use with open(...), Python handles closing automatically, so calling close() is usually unnecessary.

Example

file = open("sample.txt", "w", encoding="utf-8")
file.write("Python file handling")
file.close()

print("File closed:", file.closed)

Output:

File closed: True

Attempting to read or write using a closed file object generally raises ValueError.

14. Understanding File Paths

A file path identifies the location of a file on a computer. Python supports relative paths and absolute paths.

Relative Path

A relative path is interpreted from the program's current working directory.

with open("data.txt", "r", encoding="utf-8") as file:
    print(file.read())

Absolute Path

An absolute path specifies the file's location from a root or drive location.

with open("C:/Users/Student/Documents/data.txt", "r", encoding="utf-8") as file:
    print(file.read())

The example uses a Windows-style drive path written with forward slashes. The actual path must exist on the computer where the program runs.

Using pathlib for Paths

The pathlib module provides an object-oriented approach to working with file paths.

from pathlib import Path

file_path = Path("data") / "student.txt"

with file_path.open("r", encoding="utf-8") as file:
    print(file.read())

Using Path helps construct paths without manually joining path components using operating-system-specific separators.

15. Handling File Exceptions

File operations may fail for several reasons, including missing files, permission restrictions, invalid paths, or encoding problems. Python provides exceptions that allow programs to respond to these situations.

Exception Meaning Typical Cause
FileNotFoundError The requested file does not exist at the specified path. Opening a missing file in read mode.
PermissionError The operation is not permitted. Insufficient permissions to read or write a file.
IsADirectoryError A file operation was attempted on a directory where a file was expected. Trying to read a directory using open().
UnicodeDecodeError Bytes cannot be decoded using the selected text encoding. Reading a file with an incompatible encoding.
OSError A general operating-system-related I/O error. Some filesystem or device operations fail.
ValueError An operation is invalid for the current file object state. Attempting to read from a closed file.

Example: Handling a Missing File

try:
    with open("missing_file.txt", "r", encoding="utf-8") as file:
        content = file.read()
        print(content)
except FileNotFoundError:
    print("Error: The requested file was not found.")

Output if the file is missing:

Error: The requested file was not found.

Example: Handling Multiple File Errors

try:
    with open("report.txt", "r", encoding="utf-8") as file:
        print(file.read())
except FileNotFoundError:
    print("The report file does not exist.")
except PermissionError:
    print("You do not have permission to read the report.")
except OSError as error:
    print("An operating-system error occurred:", error)

Catch specific exceptions before broader exceptions. This makes it easier to provide the correct response for each failure. Avoid silently ignoring file errors because it can lead to missing or incomplete data.

16. Text Files and Binary Files

Python can work with text files and binary files. The mode determines how data is interpreted and returned.

Feature Text Mode Binary Mode
Example modes r, w, a rb, wb, ab
Data type Strings (str) Bytes (bytes)
Encoding Uses a character encoding, such as UTF-8. Does not decode the data into text.
Common use Text documents, CSV, JSON, and logs. Images, PDFs, audio, and other binary content.
Encoding parameter Can be specified with encoding. Should not be supplied in binary mode.

Example: Reading Binary Data

with open("image.png", "rb") as file:
    image_data = file.read()

print(type(image_data))
print("Bytes read:", len(image_data))

The file must exist for this example to run. The content is returned as bytes rather than a text string.

Example: Writing Binary Data

data = bytes([65, 66, 67])

with open("sample.bin", "wb") as file:
    file.write(data)

This writes three bytes to sample.bin. Binary mode should be used when the contents must be preserved as raw bytes.

17. The Importance of Encoding

Text encoding determines how characters are represented as bytes in a file. Specifying an encoding makes file operations more predictable across different computers and environments.

UTF-8 is widely used because it supports a broad range of characters, including English text and many other writing systems.

Example

message = "Hello, Python! नमस्ते"

with open("message.txt", "w", encoding="utf-8") as file:
    file.write(message)

with open("message.txt", "r", encoding="utf-8") as file:
    content = file.read()

print(content)

Output:

Hello, Python! नमस्ते

Using the same intended encoding for writing and reading helps prevent character corruption or decoding errors.

18. File Pointer and File Position

A file object maintains a position indicating where the next read or write operation will occur. Reading data usually advances the position.

Using tell()

The tell() method reports the current file position. In binary mode, this position is measured in bytes. In text mode, the returned value is an opaque position marker that can be used with seek().

with open("sample.txt", "w+", encoding="utf-8") as file:
    file.write("Python")
    print("Position after writing:", file.tell())

Output:

Position after writing: 6

Using seek()

The seek() method changes the file position.

with open("sample.txt", "r", encoding="utf-8") as file:
    file.seek(0)
    print(file.read())

For text files, seeking from the beginning using a valid position returned by tell() is the safest general approach. Binary files support more flexible byte-position operations.

19. Checking Whether a File Exists

Python's pathlib module provides the exists() method for checking whether a path exists.

from pathlib import Path

file_path = Path("student.txt")

if file_path.exists():
    print("The path exists.")
else:
    print("The path does not exist.")

Note: exists() can be used for both files and directories. If you specifically need to verify that a path is a regular file, use is_file().

from pathlib import Path

file_path = Path("student.txt")

if file_path.is_file():
    print("A file is available.")
else:
    print("The path is not a regular file.")

Checking existence can help make a program's behavior clearer, but a file may change between checking and opening it. You should still handle exceptions around the actual file operation.

20. Practical Example: Student Record File

This example combines writing, appending, reading, and exception handling to manage simple student records in a text file.

Step 1: Save a Student Record

student_name = "Amit"
course_name = "Python"
marks = 88

with open("students.txt", "w", encoding="utf-8") as file:
    file.write(f"Name: {student_name}\n")
    file.write(f"Course: {course_name}\n")
    file.write(f"Marks: {marks}\n")

print("Student record saved.")

Step 2: Append Another Record

with open("students.txt", "a", encoding="utf-8") as file:
    file.write("\nName: Neha\n")
    file.write("Course: Python\n")
    file.write("Marks: 92\n")

print("Another record added.")

Step 3: Read All Records

try:
    with open("students.txt", "r", encoding="utf-8") as file:
        print(file.read())
except FileNotFoundError:
    print("Student records have not been created yet.")

Sample output:

Name: Amit
Course: Python
Marks: 88

Name: Neha
Course: Python
Marks: 92

This example is suitable for learning basic file operations. For larger applications, structured formats such as CSV, JSON, or a database are usually easier to validate and query.

21. Best Practices for File Handling

  • Use the with statement to open files safely.
  • Specify encoding="utf-8" when working with text files unless another encoding is required.
  • Choose the correct file mode for the operation.
  • Remember that w truncates existing file contents.
  • Use a when adding content without replacing existing data.
  • Use binary mode for files that contain raw binary data.
  • Catch relevant exceptions such as FileNotFoundError and PermissionError.
  • Use pathlib.Path to construct and inspect file paths.
  • Process large files line by line instead of reading everything into memory.
  • Validate data before writing it to a file.
  • Avoid hardcoding environment-specific paths when a configurable path would be more suitable.
  • Be careful when overwriting files containing important data.
  • Do not store passwords, access tokens, or other secrets in plain-text files.
  • Test file operations using missing files, empty files, unusual characters, and permission restrictions.

22. Common File Handling Errors

Problem Possible Cause Solution
FileNotFoundError The file name or path is incorrect, or the file is missing. Check the path and handle the exception.
Existing data disappears The file was opened in w mode. Use a mode when appending is intended.
Text appears corrupted The file was read using an incompatible encoding. Use the correct encoding, such as UTF-8 when appropriate.
TypeError while writing The supplied data type is incompatible with the file mode. Write a string in text mode or bytes in binary mode.
ValueError on read or write The file object may already be closed. Perform file operations inside the active with block.
Only part of a file is read The current file position is not at the beginning. Use a valid seek() position or reopen the file.
New text joins the previous line The previous content does not end with a newline. Add a newline where required by the file format.

23. Practice Exercises

Exercise 1: Create a Text File

Write a Python program that creates my_notes.txt and stores five lines describing what you have learned about Python.

Exercise 2: Read File Contents

Open the file created in Exercise 1 and display its complete contents using read().

Exercise 3: Display Lines Individually

Read the same file line by line using a for loop and print each line.

Exercise 4: Append a New Line

Append your name and course name to the existing file without deleting the previous content.

Exercise 5: Handle a Missing File

Attempt to read a file that does not exist and handle FileNotFoundError gracefully.

Exercise 6: Count Lines

Write a program that counts the number of lines in a text file without loading the entire file into memory.

Exercise 7: Copy a Text File

Read the contents of one text file and write them to another file. Handle cases where the source file is missing.

24. Mini Project: Personal Notes Manager

Objective: Build a simple command-line application that stores and displays personal notes using a text file.

Project Requirements

  • Allow the user to enter a note.
  • Append the note to a file named personal_notes.txt.
  • Display all saved notes when requested.
  • Handle missing files gracefully.
  • Use UTF-8 encoding.
  • Use the with statement for every file operation.

Sample Solution

from pathlib import Path

NOTES_FILE = Path("personal_notes.txt")


def add_note(note):
    if not note.strip():
        print("Note cannot be empty.")
        return

    with NOTES_FILE.open("a", encoding="utf-8") as file:
        file.write(note.strip() + "\n")

    print("Note saved successfully.")


def show_notes():
    try:
        with NOTES_FILE.open("r", encoding="utf-8") as file:
            content = file.read()

        if content:
            print("\nSaved Notes:")
            print(content, end="" if content.endswith("\n") else "\n")
        else:
            print("No notes are available.")

    except FileNotFoundError:
        print("No notes file exists yet.")


while True:
    print("\n1. Add a note")
    print("2. View notes")
    print("3. Exit")

    choice = input("Choose an option: ").strip()

    if choice == "1":
        note = input("Enter your note: ")
        add_note(note)
    elif choice == "2":
        show_notes()
    elif choice == "3":
        print("Exiting Notes Manager.")
        break
    else:
        print("Invalid option. Please try again.")

Sample Interaction

1. Add a note
2. View notes
3. Exit
Choose an option: 1
Enter your note: Learn Python file handling
Note saved successfully.

This project demonstrates persistent storage, appending text, reading saved content, handling missing files, and using a menu-driven program.

25. Interview Questions and Answers

Q1. What is file handling in Python?

File handling is the process of opening, reading, writing, appending, and managing files from a Python program.

Q2. What is the purpose of open()?

The open() function opens a file and returns a file object that supports operations such as reading and writing.

Q3. What is the difference between w and a modes?

The w mode creates or truncates a file, while the a mode preserves existing content and directs new writes to the end.

Q4. What is the difference between read(), readline(), and readlines()?

read() reads content as a string, readline() reads one line, and readlines() returns the remaining lines as a list.

Q5. Why is the with statement recommended?

It ensures that the file is properly closed when the block exits, including when an exception occurs.

Q6. What happens if a file is opened in r mode but does not exist?

Python normally raises FileNotFoundError.

Q7. What is the difference between text mode and binary mode?

Text mode reads and writes strings using an encoding. Binary mode reads and writes bytes without text decoding.

Q8. Why should encoding be specified?

Specifying an encoding, such as UTF-8, helps ensure text is interpreted consistently across different systems.

Q9. How can you check whether a file exists?

You can use Path.exists() from the pathlib module. Use Path.is_file() if you specifically need to check for a regular file.

Q10. What is the purpose of close()?

The close() method closes an opened file and releases associated resources. When using with, closure is handled automatically.

Q11. How do you handle file-related errors?

Use try and except blocks to handle expected errors such as FileNotFoundError, PermissionError, and other relevant OSError subclasses.

Q12. How can a large text file be processed efficiently?

Iterate directly over the file object to process one line at a time instead of reading the entire file into memory.

26. Quick Revision Checklist

  • Use open() to open files.
  • Use r for reading, w for writing, and a for appending.
  • Remember that w truncates existing content.
  • Use read(), readline(), and readlines() for different reading requirements.
  • Use write() to write strings.
  • Use with to manage file resources safely.
  • Use encoding="utf-8" for text when appropriate.
  • Use binary modes for raw binary data.
  • Handle missing files and permission errors appropriately.
  • Use pathlib to work with file paths.
  • Process large files line by line.
  • Test your program with missing, empty, and invalid files.

27. Conclusion

File handling is a fundamental Python skill that allows applications to store and retrieve information beyond a program's execution. By learning how to open, read, write, append, and close files, developers can create programs that manage persistent data effectively.

Using appropriate file modes, the with statement, explicit text encodings, and exception handling helps make file operations safer and more reliable. These concepts provide a foundation for working with structured files, application logs, reports, configuration data, and larger data-processing workflows.

28. Additional Learning Resources

Explore the official Python documentation on reading and writing files for additional examples and details about file objects, file modes, and reading methods.

Explore the Python Training Course at JustAcademy, register for a course demo, or visit the JustAcademy homepage for more learning opportunities.

whatsapp