2024 25 COL100 Lab 13 File Handling
2024 25 COL100 Lab 13 File Handling
1 Introduction
File handling is an essential part of programming that allows you to create, read, write,
and manage files. Python provides built-in functions and libraries to perform file op-
erations efficiently. In this tutorial, we will explore the importance of file handling, its
different methods, and how to work with various file types such as text files (.txt) and
CSV files (.csv).
2. Exchange Information: Allow for the transfer of data between systems or users.
4. Analyze Large Datasets: Work with files containing large amounts of data, such
as CSV files.
• ‘w‘ (Write Mode): Opens a file for writing (overwriting if the file exists).
• ‘a‘ (Append Mode): Opens a file for writing but appends to the file if it exists.
• ‘r+‘ (Read and Write Mode): Opens a file for both reading and writing.
Page 2
9
1 import csv
2
Page 3
1.4.2 Reading a CSV File
1 import csv
2
1 import csv
2
11 # Writing rows
12 writer . writerow ({ " Name " : " Alice " , " Age " : 25 , " City " : "
New York " })
13 writer . writerow ({ " Name " : " Bob " , " Age " : 30 , " City " : " Los
Angeles " })
14
Page 4
1.5 Best Practices for File Handling
• Use Context Managers: Always use with open() to ensure files are closed
automatically after use.
• Handle Exceptions: Use try-except blocks to handle errors (e.g., file not found).
• Specify Correct File Paths: Ensure you use absolute or relative paths correctly.
• Choose the Right Mode: Use append mode (’a’) to add data without over-
writing and write mode (’w’) for creating new files.
• Test File Encoding: For non-English text, specify the encoding (e.g., open("file.txt",
"r", encoding="utf-8")).
1.7 Conclusion
By mastering file handling in Python, you can efficiently interact with data stored in
files, automate tasks, and handle large datasets. Use the examples above as a reference
for your projects!
Page 5
2 Submission Problem
2.1 Analyse Data
A random generator is used to create data for a student and activity management system
(SAMS). This file is provided to you as a CSV file generated data.csv with the following
structure:
1 idi , name , birth_year , birth_month , birth_day , gender , registration_id ,
athletic_score , leadership_score , talent_score , gpa , class_assigned ,
selected_activity , grade_level , talent , olympiad_scores ,
subject_specialization , fitness_score , sports_category , subject ,
mentor_grade , mentor_class , judge
This data file will contain 5036 lines. The first line follows the header stated earlier, the
next 5000 representing users, and the last 35 teachers. Your task is to write a Python
program that reads this file, processes the data, and find class assigned where there
isn’t a teacher assigned or teacher who is a judge. You need to output this data into a
new file called errors.txt, in the following format:
1 Class [ mentor_class / mentor_grade ]: Error [ TEACHER / JUDGE ] not found .
Note:
• A judge needs to be selected amongst teachers from the same class. Errors should
be shown if no teacher within a class (say 9) has judge attribute as true.
• In the case where both errors are found, first print the teacher and then the judge.
• The order of class assigned should be ascending order. That is class assigned
should be 6A, 7B, 7C...
Page 6