Python File IO: Practice Exercises

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Setup
  4. Exercise 1: Reading a File
  5. Exercise 2: Writing to a File
  6. Exercise 3: Appending to a File
  7. Summary

Introduction

In this tutorial, we will explore various file input/output (IO) operations in Python. File IO is essential for reading and writing data to files, which is a common task in many applications. By the end of this tutorial, you will learn how to read, write, and append to files using Python.

Prerequisites

To follow along with this tutorial, you should have a basic understanding of the Python programming language. Additionally, you should have Python installed on your computer.

Setup

To get started, create a new directory for our exercises and navigate to it in your terminal or command prompt: bash mkdir file-io-exercises cd file-io-exercises Inside this directory, create a new Python file named file_io_exercises.py using a text editor of your choice.

Now we are ready to dive into the practice exercises!

Exercise 1: Reading a File

In this exercise, we will read the contents of a text file with Python. Here are the steps:

  1. Create a new text file named data.txt in the same directory as the Python script.
  2. Open the file using the open() function in read mode.
  3. Read the contents of the file using the read() method.
  4. Print the contents to the console.

Let’s implement these steps in our file_io_exercises.py script: ```python # Open the file file = open(‘data.txt’, ‘r’)

# Read the contents of the file
contents = file.read()

# Print the contents
print(contents)

# Close the file
file.close()
``` Save the file and open the terminal or command prompt. Navigate to the `file-io-exercises` directory and run the script:
```bash
python file_io_exercises.py
``` If the `data.txt` file exists and contains some text, the script will read its contents and display them on the console.

Exercise 2: Writing to a File

Now, let’s move on to writing data to a file. We will create a new file named output.txt and write some content to it. Follow these steps:

  1. Open the file in write mode.
  2. Write the desired content to the file.
  3. Close the file.

Here’s the code for this exercise: ```python # Open the file in write mode file = open(‘output.txt’, ‘w’)

# Write content to the file
file.write('Hello, World!')

# Close the file
file.close()
``` Save the file and run it using the same steps as before. After running the script, you should see a new file named `output.txt` in the directory. Open this file, and you will find that it contains the text "Hello, World!".

Exercise 3: Appending to a File

In this exercise, we will learn how to append data to an existing file. Follow these steps:

  1. Open the file in append mode.
  2. Write additional content to the file.
  3. Close the file.

Here’s the code for the append exercise: ```python # Open the file in append mode file = open(‘output.txt’, ‘a’)

# Append content to the file
file.write('\nThis is a new line.')

# Close the file
file.close()
``` Save and run the script. After running it, open the `output.txt` file again. You will notice that a new line including the text "This is a new line" has been added to the file.

Summary

Congratulations! In this tutorial, we explored file IO operations in Python. We learned how to read, write, and append to files using Python scripts. Specifically, we covered the following exercises:

  1. Reading a File: Open a file, read its contents, and print them to the console.
  2. Writing to a File: Create a new file, write content to it, and save it.
  3. Appending to a File: Open an existing file, append new data to it, and save the changes.

By mastering file IO operations, you can effectively manipulate files and work with data stored in various formats. This knowledge is crucial for many real-world applications.

Feel free to explore additional file IO functionalities offered by Python, such as reading CSV files, working with binary files, or handling exceptions related to file IO. The possibilities are endless!

Remember to practice the concepts we covered in this tutorial to reinforce your understanding. Good luck with your Python file IO adventures!