Programming Exercises - 007

Exercise Programs for File Handling in Python

Below are 15 programming exercises designed to help you understand file handling in Python. These exercises include reading and writing data from lists (for matrix algebra), dictionaries (for class marks, beam information), binary files, and cover various access modes like sequential, appending, and random access.

Exercise 1: Read Matrix Data from a File

Filename: e01_read_matrix.py

Write a program that reads matrix data (list of lists) from a text file and prints it.

πŸ’‘

Tip: Use the split() method to parse each row and convert the values into integers.

Exercise 2: Write Matrix Data to a File

Filename: e02_write_matrix.py

Write a program that takes a matrix and writes it to a file. Each row of the matrix should be on a new line.

πŸ’‘

Tip: You can use join() to combine elements of the list into a formatted string for writing to the file.

Exercise 3: Append Data to a File

Filename: e03_append_data.py

Write a program that appends new data to an existing text file without overwriting the existing content.

πŸ’‘

Tip: Open the file in 'a' (append) mode to ensure the new data is added to the end of the file.

Exercise 4: Read Class Marks from a File (Dictionary)

Filename: e04_read_marks_dict.py

Write a program that reads student marks from a file and stores them in a dictionary.

πŸ’‘

Tip: Each line in the file should follow the format student_name: marks, which you can parse with split().

Exercise 5: Write Class Marks to a File (Dictionary)

Filename: e05_write_marks_dict.py

Write a program that writes a dictionary of student marks to a file. Each entry should be on a new line with the format student_name: marks.

πŸ’‘

Tip: Use the dictionary's items() method to iterate through the key-value pairs.

Exercise 6: Sequential File Access

Filename: e06_sequential_access.py

Write a program that sequentially reads and prints each line from a text file.

πŸ’‘

Tip: The for loop can directly iterate through the file object to read lines one at a time.

Exercise 7: Random File Access

Filename: e07_random_access.py

Write a program that reads a specific line from a text file, given the line number.

πŸ’‘

Tip: Use enumerate() to track line numbers while iterating through the file.

Exercise 8: Binary File Write and Read

Filename: e08_binary_file_write_read.py

Write a program to save a Python dictionary to a binary file and read it back.

πŸ’‘

Tip: Use the pickle module to serialize and deserialize Python objects in binary format.

Exercise 9: Append Dictionary Data to a File

Filename: e09_append_dict_to_file.py

Write a program that appends dictionary data to a text file in a human-readable format.

πŸ’‘

Tip: Convert the dictionary's key-value pairs to strings before writing them to the file.

Exercise 10: Read and Write Node Information (Dictionaries)

Filename: e10_read_write_nodes.py

Write a program that reads and writes node information as dictionaries. Each node should have an ID and corresponding values.

πŸ’‘

Tip: Use split() and join() to handle input/output formatting for each node.

Exercise 11: Read Beam Data from a File

Filename: e11_read_beam_data.py

Write a program that reads beam data (stored as integers) from a file and stores it in a dictionary where the keys are beam IDs.

πŸ’‘

Tip: Each line in the file should start with a beam ID, followed by a list of values representing the beam's properties.

Exercise 12: Calculate Total Force from a File

Filename: e12_total_force.py

Write a program that reads force values from a file and calculates the total force.

πŸ’‘

Tip: Convert each line to a float before adding it to the total force.

Exercise 13: Write and Read List of Beam Forces

Filename: e13_beam_forces.py

Write a program that writes a list of beam forces to a file and reads them back.

πŸ’‘

Tip: Each force should be written on a new line, and use list comprehensions to read the data back efficiently.

Exercise 14: Append Beam Forces to a File

Filename: e14_append_forces.py

Write a program that appends new beam forces to an existing file without overwriting the previous data.

πŸ’‘

Tip: Use 'a' mode to ensure that you append rather than overwrite the file.

Exercise 15: Read and Write Beam Deflection Data (CSV)

Filename: e15_beam_deflection_csv.py

Write a program that reads and writes beam deflection data to/from a CSV file. Each row represents a different deflection reading.

πŸ’‘

Tip: Use the csv module for handling CSV files, as it simplifies reading and writing rows of data.

Key Takeaways

These exercises cover different aspects of file handling in Python, including reading and writing from text files, binary files, and CSV files. By practicing these exercises, you will strengthen your understanding of file operations, which are crucial for real-world data processing tasks.

The solutions for these exercises can be found at GitHub Repository: https://github.com/jspackiaraj