Python File Handling Exercises (With Solutions)

If you want to build real-world applications, you must master Python file handling exercises and Python exception handling exercises.

Why?

Because real programs don’t just run in memory — they:

  • Read data from files

  • Write reports

  • Store logs

  • Handle unexpected errors

In this guide, you’ll solve practical exercises covering:

  • Reading and writing files

  • Working with text files

  • Handling runtime errors

  • Using try/except blocks

  • Creating custom exceptions

If you’re following a complete roadmap of Python exercises for beginners to advanced, this section moves you closer to professional-level coding.

👉 For the complete 300+ structured roadmap, read the full guide here:
Python Exercises for Beginners to Advanced (300+ Practice Problems)

Why File Handling Is Important in Python

File handling allows your program to:

  • Store persistent data

  • Process external datasets

  • Generate reports

  • Automate repetitive tasks

Most automation scripts, backend systems, and data science workflows rely heavily on file operations.

Practicing Python file handling exercises with solutions helps you become job-ready.

1️⃣ Reading from a File

Exercise 1: Read a Text File

Create a file named sample.txt and write some text inside it.

<br />
file = open(&quot;sample.txt&quot;, &quot;r&quot;)<br />
content = file.read()<br />
print(content)<br />
file.close()<br />

Better version using with:

<br />
with open(&quot;sample.txt&quot;, &quot;r&quot;) as file:<br />
    content = file.read()<br />
    print(content)<br />

Using with ensures automatic file closing — a best practice.

2️⃣ Writing to a File

Exercise 2: Write Data to a File

<br />
with open(&quot;output.txt&quot;, &quot;w&quot;) as file:<br />
    file.write(&quot;Hello, this is Python file handling practice.&quot;)<br />

This is one of the most common Python programming exercises for beginners.

3️⃣ Append Data to a File

Exercise 3: Append Content

<br />
with open(&quot;output.txt&quot;, &quot;a&quot;) as file:<br />
    file.write(&quot;\nAdding a new line.&quot;)<br />

Appending prevents overwriting existing content.

4️⃣ Handling File Not Found Error

Now let’s combine file handling with exception handling.

Exercise 4: Handle Missing File Error

<br />
try:<br />
    with open(&quot;missing.txt&quot;, &quot;r&quot;) as file:<br />
        print(file.read())<br />
except FileNotFoundError:<br />
    print(&quot;File not found. Please check the file name.&quot;)<br />

This is a basic Python try except example that every developer must understand.

5️⃣ Handling Multiple Exceptions

Exercise 5: Handle Multiple Errors

<br />
try:<br />
    number = int(input(&quot;Enter a number: &quot;))<br />
    result = 100 / number<br />
    print(result)<br />
except ZeroDivisionError:<br />
    print(&quot;Cannot divide by zero.&quot;)<br />
except ValueError:<br />
    print(&quot;Invalid input.&quot;)<br />

Practicing these Python exception handling exercises prepares you for real-world debugging.

6️⃣ Creating a Custom Exception

Advanced developers often create custom exceptions.

Exercise 6: Custom Exception Example

<br />
class NegativeValueError(Exception):<br />
    pass</p>
<p>try:<br />
    value = int(input(&quot;Enter a positive number: &quot;))<br />
    if value &lt; 0:<br />
        raise NegativeValueError(&quot;Negative values are not allowed.&quot;)<br />
except NegativeValueError as e:<br />
    print(e)<br />

This is considered an advanced Python exercise and frequently appears in interviews.

How File and Exception Handling Improve Your Coding Skills

By solving structured Python file handling and error handling practice problems, you:

  • Build robust applications

  • Prevent program crashes

  • Improve debugging skills

  • Write production-level code

  • Prepare for backend development

But this is only one part of the full Python journey.

If you want to master:

  • Data structure exercises

  • OOP challenges

  • GUI programming

  • NumPy & Pandas practice

  • Django web development

👉 Read the complete roadmap here:
Python Exercises for Beginners to Advanced (300+ Practice Problems)

Want 300+ Structured Python Exercises?

These examples are only a small preview.

The complete course includes:

  • 300+ Python exercises with solutions

  • Beginner to advanced progression

  • OOP mastery

  • Django practice

  • NumPy & Pandas exercises

  • GUI development

  • Real-world coding challenges

Created by Faisal Zamir – Udemy Instructor from Pakistan, trusted by 400,000+ students worldwide.

If you’re searching for:

  • Python file handling exercises with solutions

  • Python exception handling practice

  • Best Python course on Udemy

  • Faisal Zamir Udemy instructor

This structured course is designed for serious learners.

👉 Enroll now on Udemy: Python 300+ Exercises with Solutions

Leave a Comment

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

Scroll to Top