File Handling

File Handling

Part 6 : Python for Scripting

ยท

7 min read

Welcome to Part 6 of the "Python for Scripting" series! In this installment, we'll explore the essential concepts of file handling in Python. Whether you're a beginner or an experienced Python user, understanding how to read, write, and manage files is a fundamental skill. We'll cover the following topics:

  1. Opening and Reading Files

    • Using open()

    • Reading file contents

  2. Writing to Files

    • Writing text to a file

    • Appending to a file

  3. Working with Different File Modes

    • Read mode (r)

    • Write mode (w)

    • Append mode (a)

  4. Handling Exceptions

    • Using try, except, and finally
  5. Best Practices for File Handling

1. Opening and Reading Files

Using open()

To work with files in Python, you first need to open them using the open() function. The basic syntax is as follows:

file = open("filename", "mode")

Here, "filename" is the name of the file you want to open, and "mode" is a string indicating how you want to open the file (e.g., for reading, writing, or appending).

Reading File Contents

Once a file is open, you can read its contents. The read() method reads the entire file, while the readline() method reads a single line at a time.

# Reading the entire file
with open("sample.txt", "r") as file:
    content = file.read()
    print(content)

2. Writing to Files

Writing Text to a File

To write data to a file, you can open it in write mode ("w") and use the write() method.

# Writing text to a file
with open("output.txt", "w") as file:
    file.write("This is some sample text.\n")
    file.write("Here's another line of text.")

Appending to a File

To add content to an existing file, open it in append mode ("a").

# Appending text to a file
with open("output.txt", "a") as file:
    file.write("\nThis text is appended to the file.")

3. Working with Different File Modes

Read Mode (r)

The read mode ("r") allows you to open files for reading only. Attempting to write to a file opened in this mode will result in an error.

with open("sample.txt", "r") as file:
    content = file.read()
    print(content)

Write Mode (w)

The write mode ("w") allows you to create a new file for writing. If the file already exists, it will be overwritten. Be careful when using this mode.

with open("output.txt", "w") as file:
    file.write("This is a new file.")

Append Mode (a)

The append mode ("a") allows you to open files for writing, but it doesn't overwrite the file. Instead, it appends new data to the end of the existing content.

with open("output.txt", "a") as file:
    file.write("\nThis is appended to the file.")

4. Handling Exceptions

Using try, except, and finally

When working with files, exceptions can occur, such as file not found or permission issues. You can use try, except, and finally to handle exceptions gracefully.

try:
    with open("missing_file.txt", "r") as file:
        content = file.read()
        print(content)
except FileNotFoundError:
    print("The file does not exist.")
finally:
    print("File handling is done.")

5. Best Practices for File Handling

  • Always use the with statement to open and work with files. It ensures that the file is properly closed when you're done.

  • Be cautious when using write mode ("w") as it can overwrite existing files.

  • Use descriptive file and variable names for clarity.

  • Handle exceptions to prevent crashes due to unexpected errors.

Solutions to Exercises from the Previous Blog

Exercise 1: Calculating the Area and Circumference of a Circle

Solution:

math_operations.py:

import math

def calculate_area(radius):
   #Calculate the area of a circle.
    area = math.pi * (radius**2)
    return area

def calculate_circumference(radius):
    #Calculate the circumference of a circle.
    circumference = 2 * math.pi * radius
    return circumference

script.py:

import math_operations

radius = 5
area = math_operations.calculate_area(radius)
circumference = math_operations.calculate_circumference(radius)

print(f"Area of the circle with a radius of {radius} units: {area:.2f} square units")
print(f"Circumference of the circle with a radius of {radius} units: {circumference:.2f} units")

Explanation:

In this exercise, we created a Python module named math_operations that contains two functions: calculate_area and calculate_circumference. These functions take the radius of a circle as a parameter and calculate the area and circumference, respectively.

In the script.py script, we imported the math_operations module and used the functions to calculate and print the area and circumference of a circle with a radius of 5 units. We used the mathematical constant math.pi to calculate these values.

Exercise 2: String Operations

Solution:

string_utilities.py:

def count_words(text):
    """Count the number of words in a string."""
    words = text.split()
    return len(words)

def reverse_string(text):
    """Reverse a string."""
    return text[::-1]

def is_palindrome(text):
    """Check if a string is a palindrome."""
    clean_text = ''.join(text.split()).lower()
    return clean_text == clean_text[::-1]

script.py:

import string_utilities

sample_string = "racecar"
word_count = string_utilities.count_words("This is a sample sentence.")
reversed_text = string_utilities.reverse_string(sample_string)
is_palindrome = string_utilities.is_palindrome(sample_string)

print(f"Word count: {word_count}")
print(f"Reversed string: {reversed_text}")
print(f"Is '{sample_string}' a palindrome? {is_palindrome}")

Explanation:

In this exercise, we created a Python module named string_utilities that contains three functions: count_words, reverse_string, and is_palindrome. These functions perform common string operations.

In the script.py script, we imported the string_utilities module and used the functions to demonstrate these operations. We counted the number of words in a sentence, reversed a string, and checked if a given string is a palindrome.

Now, let's proceed to solve the remaining exercises from the "Modules and Functions" blog:

Exercise 3: Reading and Writing Text Files

Solution:

file_io.py:

def read_text_file(file_name):
    """Read the contents of a text file and return them as a list of lines."""
    with open(file_name, "r") as file:
        lines = file.readlines()
    return lines

def write_text_file(file_name, lines):
    """Write a list of strings to a new text file."""
    with open(file_name, "w") as file:
        for line in lines:
            file.write(line + '\n')

script.py:

import file_io

# Read the contents of a text file
file_name = "sample.txt"
file_contents = file_io.read_text_file(file_name)

# Write the content to a new file
new_file_name = "new_sample.txt"
file_io.write_text_file(new_file_name, file_contents)

Explanation:

In this exercise, we created a Python module named file_io that contains two functions: read_text_file and write_text_file. The read_text_file function reads the contents of a text file and returns them as a list of lines. The write_text_file function writes a list of strings to a new text file.

In the script.py script, we imported the file_io module, read the contents of a sample text file, and then wrote the same content to a new file with a different name.

Exercise 4: Data Analysis

Solution:

data_analysis.py:

from statistics import mean, median, mode

def calculate_mean(numbers):
    """Calculate the mean of a list of numbers."""
    return mean(numbers)

def calculate_median(numbers):
    """Calculate the median of a list of numbers."""
    return median(numbers)

def calculate_mode(numbers):
    """Calculate the mode of a list of numbers."""
    return mode(numbers)

script.py:

import data_analysis

data = [10, 20, 30, 20, 40, 20, 10, 50]

mean_value = data_analysis.calculate_mean(data)
median_value = data_analysis.calculate_median(data)
mode_value = data_analysis.calculate_mode(data)

print(f"Mean: {mean_value}")
print(f"Median: {median_value}")
print(f"Mode: {mode_value}")

Explanation:

In this exercise, we created a Python module named data_analysis that uses the statistics module to calculate the mean, median, and mode of a list of numbers.

In the script.py script, we imported the data_analysis module and used its functions to calculate and print the mean, median, and mode of a list of data.

Exercise 5: Web Scraping

Solution:

web_scraper.py:

import requests
from bs4 import BeautifulSoup

def scrape_website(url):
    #Scrape data from a website and return the content.
    response = requests.get(url)
    if response.status_code == 200:
        soup = BeautifulSoup(response.text, "html.parser")
        content = soup.get_text()
        return

 content
    else:
        return "Failed to retrieve data from the website."

# Example usage
if __name__ == "__main__":
    url = "https://example.com"
    web_content = scrape_website(url)
    print(web_content)

Explanation:

In this exercise, we created a Python module named web_scraper that uses the requests and BeautifulSoup libraries to scrape data from a website. The scrape_website function retrieves the content from a specified URL and returns it.

The example usage provided in the module demonstrates how to use the scrape_website function to scrape data from a website.

These solutions and explanations should help you understand how to implement and use these modules and functions effectively. You can apply these concepts to a wide range of real-world scenarios, whether you're working with files, data analysis, web scraping, or any other Python project.

Exercises

Python Exercises and Interesting Codes - TechNData - Tech & Data

Now, let's put your file handling skills to the test with some exercises:

Exercise 1: Create a Python script that reads the contents of a text file and counts the number of words in it.

Exercise 2: Write a Python program that takes user input and appends it to an existing file.

Exercise 3: Create a function that accepts a file name and a string, and writes the string to the file in append mode.

Exercise 4: Develop a Python script that reads a CSV file and prints its contents as a table.

Exercise 5: Build a program that searches for a specific word in a text file and displays the line and line number where the word is found.

These exercises will help you practice and reinforce your file-handling skills in Python. Feel free to share your solutions with others and continue your Python scripting journey. Stay tuned for more advanced topics in our series! ๐Ÿš€ #PythonScripting #FileHandling #CodingExercises

Did you find this article valuable?

Support Dhananjay Kulkarni by becoming a sponsor. Any amount is appreciated!

ย