CHAPTER 3 Python Modules and File Handling Chapter Introduction / Overview Python becomes powerful when programs are organised into reusable parts. Instead of writing all code in one long file, developers divide programs into modules, packages, and libraries. A module is a Python file that contains functions, classes, variables, and executable statements that can be imported and reused in other programs. This chapter introduces the practical foundations of Python modules and file handling. It explains how to create modules, import them in different ways, rename modules using aliases, access variables stored in modules, use built-in modules, and inspect module contents using the dir() function. The chapter then moves to data exchange and text processing using JSON and Regular Expressions. JSON is widely used for APIs, configuration files, and web applications, while regular expressions help search, validate, extract, and replace text patterns. Finally, the chapter covers exception handling and file handling, both of which are essential for writing reliable programs that can work with real-world data and unexpected errors. Learning Outcomes Upon successful completion of this chapter, students will be able to: 1. Explain the meaning and importance of Python modules in program organisation and code reuse. 2. Create user-defined modules and use them in another Python program. 3. Apply correct module naming rules and rename imported modules using aliases. 4. Access variables defined inside modules and use common built-in modules effectively. 5. Inspect module contents using the dir() function and compare different import techniques. 6. Parse JSON strings into Python objects and convert Python data into JSON format. 7. Use regular expressions for searching, matching, extracting, splitting, and replacing text. 8. Handle exceptions using try-except, multiple except blocks, else, finally, and custom error messages. 9. Perform file operations such as opening, reading, writing, appending, and closing files safely. 10. Combine modules, JSON, RegEx, exception handling, and file handling in small practical programs. Key Concepts Glossary of Key Terms in This Chapter Module: A Python file with a .py extension that contains reusable code such as functions, variables, and classes. Package: A folder that groups related modules. It usually contains an __init__.py file in traditional package structure. Library: A collection of modules and packages designed for specific tasks such as mathematics, web development, data analysis, or automation. Import: The process of bringing code from one module into another program. Alias: An alternate short name given to an imported module or object using the as keyword. Built - in Module: A module included with Python installation, such as math, random, os, sys, json, re, and datetime. dir() Function: A built-in function that lists the names and attributes available inside an object or module. JSON: JavaScript Object Notation, a lightweight text format used to store and exchange structured data. Regular Expression: A pattern language used to search, validate, extract, and replace text. Exception: An error detected during program execution that can be handled using try-except blocks. File Handling: The process of opening, reading, writing, appending, and closing files using Python programs. 3.1 Introduction to Python Modules A Python module is a file containing Python definitions and statements. The file name becomes the module name without the .py extension. For example, a file named calculator.py becomes a module named calculator. Modules allow programmers to organise code logically. A large program can be divided into smaller files, where each file handles one responsibility. This makes the program easier to read, test, debug, and maintain. Python already provides many built-in modules, and programmers can also create their own modules. A module can contain functions, classes, constants, configuration values, and executable code. Key Insight: A module is not a separate programming language. It is simply a Python file that can be reused by importing it into another Python file. Figure 3.1: Python module workflow from creation to reuse The Python Module Workflow diagram illustrates the sequence of steps involved in creating a Python module and making it reusable in different projects. 1. Create Module The process begins by creating a Python module. A module is simply a Python file that contains reusable code. During this stage, the developer writes the required functions, classes, and variables that perform specific tasks. The goal is to organize related functionality into a single file for easier maintenance and reuse. 2. Write & Test Once the module is created, it is thoroughly tested to ensure that it works as expected. Different inputs are used to verify the correctness of the functions, identify errors, and improve reliability. Testing helps detect bugs early and ensures that the module performs accurately before it is shared or used in larger applications. 3. Package Module After successful testing, the module is organized into a package. This involves arranging the project files in a standard directory structure and including the necessary configuration and metadata files. Packaging makes the module easier to distribute, install, and maintain. 4. Distribute The packaged module is then made available for use. It can be shared through a public repository such as the Python Package Index (PyPI) or stored in a private repository within an organization. Distribution allows other developers or applications to install and use the module without manually copying the source code. 5. Import & Reuse Once distributed, the module can be imported into multiple Python programs. Developers can use its existing functions and classes without rewriting the same code. This promotes code reuse, reduces development time, and ensures consistency across different projects. Iterate & Improve The workflow is not a one-time process. After the module is used, developers may discover bugs, identify opportunities for optimization, or decide to add new features. The module is then updated, tested again, repackaged, and redistributed as a newer version. This continuous improvement cycle keeps the module reliable, efficient, and up to date. Table 3.2: Importance of modules in Python programming Reason for Using Modules Explanation Example Code reuse Write once and use in many programs. Use the same tax calculation function in multiple billing programs. Organisation Separate a large program into smaller logical files. Keep database code, validation code, and report code in separate modules. Maintainability Changes can be made in one module without rewriting the full application. Update one email-sending module used across the project. Testing Individual modules can be tested independently. Test calculator functions before using them in the main program. Teamwork Different developers can work on different modules. One developer works on login.py while another works on reports.py. 3.1.1 Module, Package and Library Although the words module, package, and library are sometimes used together, they refer to different levels of code organisation. Table 3.3: Difference between module, package and library Concept Meaning Simple Example Module One Python file. math_tools.py Package A folder containing related modules. student_package containing marks.py and attendance.py Library A larger collection of modules and packages. NumPy, Pandas, Matplotlib 3.2 Creating and Using Modules A user-defined module can be created by saving Python code in a .py file. Another Python program can then import that file and use its functions or variables. Both files should usually be in the same folder for beginner-level programs. 3.2.1 Steps to Create a Module 1. Create a new Python file with a meaningful name, such as mymath.py. 2. Define functions, variables, or classes inside the file. 3. Save the file in the same folder as the program that will use it. 4. Use the import statement in another Python file. 5. Access module members using dot notation such as module_name.function_name(). # File name: calculator_tools.py def add(a, b): return a + b def subtract(a, b): return a - b PI = 3.14159 # File name: main.py import calculator_tools print(calculator_tools.add(10, 5)) print(calculator_tools.subtract(10, 5)) print(calculator_tools.PI) Table 3.4: Explanation of a basic user-defined module 3.2.2 Best Practices for Creating Modules ● Keep each module focused on one main purpose. ● Use meaningful function and variable names. ● Avoid writing too much executable code directly at module level. ● Add comments or docstrings to explain important functions. ● Test the module independently before using it in a larger project. Part of Program Role calculator_tools.py Module file containing reusable functions and variables. main.py Main program that imports and uses the module. import calculator_tools Loads the module into the program. calculator_tools.add(10, 5) Calls the add function using dot notation. 3.3 Naming and Renaming Modules A module name should be simple, readable, and valid as a Python identifier. Good naming reduces confusion and prevents import errors. Table 3.5: Module naming rules and examples Rule Correct Example Incorrect Example Use lowercase letters student_records.py StudentRecords.py Use underscores for readability file_utils.py file-utils.py Do not begin with a number module3_notes.py 3module.py Avoid spaces data_cleaning.py data cleaning.py Avoid names of built-in modules my_math_tools.py math.py 3.3.1 Renaming Modules Using Aliases Python allows an imported module to be given a shorter or more convenient name using the as keyword. This does not rename the original file; it only creates an alias inside the current program. import math as m print(m.sqrt(25)) print(m.pi) Aliases are commonly used for popular libraries. For example, NumPy is usually imported as np, Pandas as pd, and Matplotlib pyplot as plt. import random as rd number = rd.randint(1, 10) print(number) Important: Do not create files named math.py, random.py, json.py, or re.py in your project folder. These names can hide the original built-in modules and cause confusing errors. 3.4 Variables in Modules, Built-in Modules and dir() Function 3.4.1 Variables in Modules A module can store variables just like a normal Python program. These variables can be accessed after importing the module. Module-level variables are useful for constants, configuration values, version numbers, and shared settings. # File name: college_info.py college_name = "ABC Institute" course = "Python Programming" semester = 5 # File name: main.py import college_info print(college_info.college_name) print(college_info.course) print(college_info.semester) Good Practice: Constants are often written in uppercase letters, such as PI = 3.14159 or MAX_ATTEMPTS = 3, to show that they should not be changed casually. 3.4.2 Built-in Modules Python includes many ready-to-use modules in its standard library. These modules help perform common tasks without installing extra packages. Table 3.6: Common built-in modules in Python import math import random from datetime import date print(math.sqrt(64)) print(random.choice(["red", "blue", "green"])) print(date.today()) 3.4.3 The dir() Function The dir() function returns a list of names available inside an object or module. It is useful for exploring what functions, classes, constants, and attributes are provided by a module. import math print(dir(math)) print(math.sqrt(16)) Table 3.7: Common uses of dir() function Use of dir() Meaning dir(math) Displays names available in the math module. dir(str) Displays methods available for string objects. dir() Displays names available in the current scope. 3.5 Importing from Modules Python provides different ways to import modules depending on how much of the module is required. Choosing the correct import style improves readability and prevents name conflicts. Table 3.8: Types of import statements in Python Import Style Syntax When to Use Built-in Module Purpose Example Use math Mathematical functions and constants. sqrt(), ceil(), pi random Random number generation. randint(), choice(), shuffle() datetime Dates and times. date.today(), datetime.now() os Operating system interaction. listdir(), mkdir(), path.exists() sys Python interpreter information. sys.version, sys.path json JSON parsing and conversion. loads(), dumps(), load(), dump() re Regular expression operations. search(), findall(), sub() statistics Basic statistics. mean(), median(), mode() Import full module import math When many functions from the module are required. Import with alias import math as m When a shorter name improves readability. Import one name from math import sqrt When only one function or variable is needed. Import multiple names from math import sqrt, pi When a few selected names are needed. Import all names from math import * Generally avoided because it can create name conflicts. import math print(math.sqrt(81)) from math import sqrt, pi print(sqrt(81)) print(pi) import math as m print(m.factorial(5)) 3.5.1 The __name__ Variable When a Python file is run directly, its special variable __name__ is set to "__main__". When the same file is imported as a module, __name__ becomes the module name. This allows a file to contain test code that runs only when the file is executed directly. # File name: greetings.py def welcome(name): return "Welcome, " + name if __name__ == "__main__": print(welcome("Student")) Why it matters: The __name__ == "__main__" pattern prevents test code from running automatically when a module is imported into another program. 3.6 JSON in Python JSON stands for JavaScript Object Notation. It is a lightweight text format used for storing and exchanging structured data. JSON is widely used in web applications, APIs, configuration files, databases, and data transfer between systems. Figure 3.2: JSON processing workflow in Python 3.6.1 JSON Data Types and Python Mapping Table 3.9: JSON to Python data type conversion JSON Type Python Equivalent Example object dict {"name": "Amit"} array list [10, 20, 30] string str "Python" number int or float 25 or 3.14 true / false True / False true becomes True null None null becomes None 3.6.2 The json Module Python provides the built-in json module for working with JSON data. The two most common operations are parsing JSON into Python objects and converting Python objects into JSON strings. Table 3.10: Important functions in the json module Function Purpose json.loads() Parses a JSON string and converts it into a Python object. json.dumps() Converts a Python object into a JSON-formatted string. json.load() Reads JSON data from a file and converts it into a Python object. json.dump() Writes a Python object into a file in JSON format. import json student_json = '{"name": "Ravi", "age": 21, "marks": 88}' student = json.loads(student_json) print(student["name"]) print(student["marks"]) import json student = { "name": "Ravi", "age": 21, "marks": 88 } json_text = json.dumps(student, indent=4) print(json_text) 3.7 Parsing and Converting JSON Data Parsing means reading JSON text and converting it into Python data structures. Converting means taking Python data and producing JSON text or storing it in a JSON file. Both operations are common when working with APIs and configuration files. 3.7.1 Parsing Nested JSON import json data = """ { "student": { "name": "Meena", "age": 20, "subjects": ["Python", "DBMS", "AI"] } } """ obj = json.loads(data) print(obj["student"]["name"]) print(obj["student"]["subjects"][0]) 3.7.2 Reading and Writing JSON Files import json student = { "name": "Meena", "semester": 5, "skills": ["Python", "SQL"] } with open("student.json", "w") as file: json.dump(student, file, indent=4) with open("student.json", "r") as file: data = json.load(file) print(data["skills"]) Table 3.11: JSON errors and solutions Common JSON Error Reason Solution JSONDecodeError Invalid JSON syntax such as missing quotes or extra comma. Validate JSON format before parsing. KeyError Trying to access a key that does not exist. Use get() or check if key exists. TypeError Trying to serialize an unsupported Python object. Convert object to a serializable type. # Safer way to read optional values name = data.get("name", "Unknown") print(name) 3.8 Regular Expressions and RegEx Functions A Regular Expression, commonly called RegEx, is a pattern used to match text. It is useful for validating input, extracting values, searching documents, cleaning text, and replacing unwanted content. Figure 3.3: Regular expression workflow for text processing 3.8.1 RegEx Metacharacters Table 3.12: Important RegEx metacharacters Symbol Meaning Example Matches any single character except newline. a.c matches abc, axc ^ Matches the start of a string. ^Hello $ Matches the end of a string. end$ * Matches zero or more repetitions. ab* matches a, ab, abb + Matches one or more repetitions. ab+ matches ab, abb ? Matches zero or one repetition. colou?r matches color or colour [] Matches one character from a set. [aeiou] \d Matches a digit. \d+ matches 123 \w Matches a word character. \w+ \s Matches whitespace. \s+ 3.8.2 Common Functions in the re Module Table 3.13: Common functions in the re module Function Purpose re.search(pattern, text) Searches anywhere in the string and returns the first match. re.match(pattern, text) Checks for a match only at the beginning of the string. re.fullmatch(pattern, text) Checks whether the entire string matches the pattern. re.findall(pattern, text) Returns all matching substrings as a list. re.finditer(pattern, text) Returns match objects one by one for all matches. re.split(pattern, text) Splits a string based on the pattern. re.sub(pattern, replacement, text) Replaces matching text with new text. re.compile(pattern) Compiles a pattern for repeated use. import re text = "My marks are 85 and my attendance is 92" numbers = re.findall(r"\d+", text) print(numbers) import re email = "student@example.com" pattern = r"^[\w.-]+@[\w.-]+\.\w+$" if re.fullmatch(pattern, email): print("Valid email") else: print("Invalid email") import re sentence = "Python is easy" cleaned = re.sub(r"\s+", " ", sentence) print(cleaned) Raw Strings: RegEx patterns are usually written as raw strings using r"pattern". This avoids confusion with Python escape characters such as \n and \t. 3.9 Exception Handling and Multiple Exceptions An exception is an error that occurs while a program is running. Without exception handling, the program stops immediately. With exception handling, the program can display a meaningful message, recover from the error, or close resources safely. 3.9.1 Basic try-except Structure try: number = int(input("Enter a number: ")) print(100 / number) except ValueError: print("Please enter only numbers.") except ZeroDivisionError: print("Cannot divide by zero.") 3.9.2 Multiple Exceptions Multiple except blocks are used when different errors require different responses. Python checks the except blocks from top to bottom and runs the first matching block. Table 3.14: Common exceptions in Python Exception When It Occurs Example ValueError Correct type but invalid value. int("abc") ZeroDivisionError Division by zero. 10 / 0 FileNotFoundError File does not exist. open("missing.txt") KeyError Dictionary key is missing. student["age"] when age is absent IndexError List index is out of range. items[10] TypeError Operation on incompatible types. "5" + 2 3.9.3 else and finally Blocks try: file = open("data.txt", "r") content = file.read() except FileNotFoundError: print("File not found.") else: print("File read successfully.") finally: print("Program finished.") The else block runs only if no exception occurs. The finally block runs whether an exception occurs or not. It is commonly used to close files, release resources, or display final messages. 3.9.4 Raising Exceptions def set_age(age): if age < 0: raise ValueError("Age cannot be negative") return age print(set_age(20)) Best Practice: Avoid using a bare except: block because it catches all errors and can hide programming mistakes. Catch specific exceptions whenever possible. 3.10 File Handling Concepts File handling allows a program to store data permanently and read data from external sources. Python can work with text files, CSV files, JSON files, binary files, logs, and many other file formats. Figure 3.4: Safe file handling workflow in Python 3.10.1 Opening and Closing Files The open() function is used to open a file. It returns a file object. Files should be closed after use to avoid resource leakage. The recommended method is to use the with statement because it closes the file automatically. with open("notes.txt", "r") as file: content = file.read() print(content) Table 3.15: File opening modes in Python Mode Meaning Use Case r Read mode. File must exist. Read an existing text file. w Write mode. Creates or overwrites file. Save new output. a Append mode. Adds content at the end. Add log messages. x Create mode. Fails if file exists. Create a new file safely. b Binary mode. Read images, audio, or binary data. t Text mode. Default mode. Read normal text files. + Read and write mode. Update file content. 3.10.2 Reading Files with open("students.txt", "r") as file: print(file.read()) # reads full file with open("students.txt", "r") as file: print(file.readline()) # reads one line with open("students.txt", "r") as file: for line in file: print(line.strip()) 3.10.3 Writing and Appending Files with open("output.txt", "w") as file: file.write("First line\n") file.write("Second line\n") with open("output.txt", "a") as file: file.write("Appended line\n") 3.10.4 Working with File Paths and Encoding A file path tells Python where the file is located. If only the file name is given, Python searches in the current working directory. For text files, UTF-8 encoding is commonly used because it supports many languages and symbols. 3.10.5 File Handling with Exceptions try: with open("marks.txt", "r") as file: marks = file.read() print(marks) except FileNotFoundError: print("The file marks.txt was not found.") from pathlib import Path path = Path("data") / "students.txt" with open(path, "r", encoding="utf-8") as file: content = file.read() print(content) except PermissionError: print("You do not have permission to read this file.") Table 3.16: Important file handling concepts Concept Explanation Current working directory The folder from which the Python program is currently running. Absolute path Full path from the root of the system. Relative path Path relative to the current working directory. Encoding Rule used to convert text into bytes and bytes into text. Buffering Temporary storage used while reading or writing data. 3.11 Integrated Practical Example The following example combines important concepts from this unit: importing modules, reading JSON data, validating email using RegEx, handling exceptions, and writing output to a file. import json import re EMAIL_PATTERN = r"^[\w.-]+@[\w.-]+\.\w+$" try: with open("students.json", "r", encoding="utf-8") as file: students = json.load(file) valid_students = [] for student in students: email = student.get("email", "") if re.fullmatch(EMAIL_PATTERN, email): valid_students.append(student) with open("valid_students.json", "w", encoding="utf-8") as file: json.dump(valid_students, file, indent=4) print("Valid student records saved successfully.") except FileNotFoundError: print("Input JSON file not found.") except json.JSONDecodeError: print("Invalid JSON format.") except Exception as error: print("Unexpected error:", error) Table 3.17: Concepts demonstrated in the integrated example Concept Used Where It Appears in the Program Module import json and re modules are imported at the beginning. JSON parsing json.load(file) reads JSON from a file into Python objects. RegEx validation re.fullmatch() checks whether the email format is valid. File writing json.dump() writes valid records into a new JSON file. Exception handling Specific except blocks handle missing file and invalid JSON. 3.11.1 Unit Summary ● Modules help divide programs into reusable and maintainable files. ● Import statements allow access to functions, classes, and variables from other modules. ● The dir() function helps explore module contents. ● JSON is used for structured data exchange and can be parsed using the json module. ● Regular expressions support advanced text searching, validation, extraction, splitting, and replacement. ● Exception handling prevents sudden program termination and improves user-friendly error reporting. ● File handling allows programs to read and write permanent data safely using with open(). 3.12 Further Reading / Viewing The following resources can help students revise and extend their understanding of Python modules, JSON, RegEx, exceptions, and file handling. Table 3.18: Recommended learning resources for Unit 3 Resource Type Suggested Resource / Topic Purpose Official documentation Python documentation: Modules Understand import system and module organisation. Official documentation Python json module documentation Learn json.load, json.loads, json.dump, and json.dumps. Official documentation Python re module documentation Study RegEx functions and syntax. Tutorial practice W3Schools / Programiz Python Modules, JSON, RegEx and File Handling Beginner-friendly examples and exercises. Practice platform HackerRank / LeetCode beginner Python problems Practice functions, strings, files, and errors. Video learning Python file handling and exception handling tutorials Visual explanation with coding demonstrations. 3.13 Assessment Questions Part A - Multiple Choice Questions (1 Mark Each) Q1. Which file extension is used for a Python module? a. .txt b. .py c. .json d. .exe Answer: (b) Q2. Which keyword is used to import a module in Python? a. include b. import c. module d. using Answer: (b) Q3. What is the purpose of the as keyword in import statements? a. Delete a module b. Rename the Python file permanently c. Create an alias for the imported module d. Convert a module into JSON Answer: (c) Q4. Which function lists names available inside a module? a. list() b. names() c. dir() d. show() Answer: (c) Q5. Which json function converts a JSON string into a Python object? a. json.dumps() b. json.loads() c. json.dump() d. json.convert() Answer: (b) Q6. Which json function writes a Python object into a JSON file? a. json.write() b. json.save() c. json.dump() d. json.loads() Answer: (c) Q7. Which module is used for regular expressions in Python? a. regex b. re c. string d. pattern Answer: (b) Q8. Which RegEx function replaces matching text? a. re.sub() b. re.replace() c. re.change() d. re.swap() Answer: (a) Q9. Which exception occurs when a program tries to divide by zero? a. ValueError b. TypeError c. ZeroDivisionError d. IndexError Answer: (c) Q10. Which file mode is used to append data to an existing file? a. r b. w c. a d. x Answer: (c) Part B - Short Answer Questions (5 Marks Each) Q11. Define a Python module. Explain any four advantages of using modules in Python programs. Q12. Explain the difference between import module, import module as alias, and from module import name with examples. Q13. What is JSON? Explain json.loads(), json.dumps(), json.load(), and json.dump() with suitable examples. Q14. Explain any five RegEx metacharacters and their use in text processing. Q15. Write short notes on try, except, else, finally, and multiple exception handling in Python. Q16. Explain different file opening modes in Python with examples. Part C - Long Answer / Essay Questions (10 Marks Each) Q17. Discuss Python modules in detail. Include creating user-defined modules, using built-in modules, module aliases, variables in modules, and the dir() function. Q18. Explain JSON handling in Python. Describe parsing JSON strings, reading JSON files, converting Python objects to JSON, writing JSON files, and handling JSON-related errors. Q19. Describe Regular Expressions in Python. Explain the re module functions search(), match(), fullmatch(), findall(), split(), sub(), and compile() with examples. Q20. Explain exception handling and file handling together by writing a program that reads a file, processes data, handles missing files, and writes output safely. Part D - Analytical / Case-Based Questions Q21 (Case Study). A college stores student data in a JSON file. Each record contains name, register number, email, and marks. Design a Python program that reads the JSON file, validates email addresses using RegEx, handles file and JSON errors, and saves only valid records into a new file. Explain each step. Q22 (Compare and Analyse). Compare modules, JSON processing, RegEx, exception handling, and file handling in a table. Explain how these concepts can be combined in a real-world Python application such as log analysis or student record management.