Advanced Python & File Handling – The Complete Masterclass

1. The Power of Functions: Modular Programming

In the real world, software is millions of lines long. We cannot write it in one file. We use Decomposition to break it into small, manageable chunks called Functions.

1.1 Anatomy of a Function

A function has a Header and a Body.

Python

def calculate_area(radius):      # Header (Signature)
    """Returns area of circle""" # Docstring (Documentation)
    area = 3.14 * radius * radius
    return area                  # Return Statement
  • def: Keyword to define a function.
  • radius: The Parameter (Placeholder variable).
  • Indent: Everything indented is the “Body.”
  • return: Stops the function and sends value back.

1.2 Void vs. Non-Void Functions

  • Non-Void (Fruitful) Function: Returns a value (like the example above).
  • Void Function: Does not return a value. It performs an action (like printing).
    • Exam Note: A void function technically returns None in Python.

1.3 Flow of Execution

Python executes code line-by-line. When a function is called, the flow jumps to the function body, executes it, and jumps back to where it left off.

  • Exam Trick: If you define a function but never call it, the code inside never runs!

2. Arguments & Parameters: The Handshake

2.1 Parameters vs. Arguments

  • Parameter: Variable in the function definition (def func(x):).
  • Argument: Actual value passed during the call (func(10)).

2.2 The 3 Types of Arguments

  1. Positional Arguments:
    • The most common. Order matters.
    • def power(a, b): $\rightarrow$ power(2, 3) means \(a=2, b=3 \).
  2. Keyword Arguments (Named Args):
    • Order doesn’t matter if you use names.
    • power(b=3, a=2) works perfectly.
  3. Default Arguments:
    • You can provide a backup value.
    • def greet(name, msg="Good Morning"):
    • Crucial Rule: Non-default arguments must come before default arguments. def f(a=1, b) is a SyntaxError.

2.3 Pass by Object Reference (The “Mutable” Trap)

Does Python pass by Value or Reference? It uses “Pass by Object Reference”.

  • Immutable Args (Int, String): Changes inside function do not affect the outside variable.
  • Mutable Args (List, Dict): Changes inside function do affect the outside variable.

Example:

Python

def modify(L):
    L.append(100)  # Modifies the original list!

my_list = [1, 2]

modify(my_list)

print(my_list) # Output: [1, 2, 100]


3. Variable Scope: The LEGB Rule

Scope tells us where a variable is “visible.” Python resolves names using the LEGB Rule (checked in this order):

  1. L – Local: Inside the current function.
  2. E – Enclosing: Inside enclosing functions (nested functions).
  3. G – Global: At the top level of the script.
  4. B – Built-in: Python’s built-in names (like len, print).

The global Keyword

To write to a global variable inside a function, you must declare it.

Python

x = 10
def change():
    global x  # Permission to modify global x
    x = 20
change()
print(x) # Output: 20

4. File Handling: Persistence

Files allow data to survive after the program ends.

4.1 Absolute vs. Relative Paths

  • Absolute Path: The full address from root. C:\Users\Admin\Documents\data.txt
  • Relative Path: Address relative to the current script. data.txt or folder/data.txt.

4.2 The open() Function and Modes

Syntax: file_object = open(file_name, access_mode)

ModeNameDescriptionPointer
'r'ReadDefault. Error if file missing.Start
'w'WriteCreates/Overwrites file.Start
'a'AppendWrites at end. Preserves old data.End
'r+'Read+WriteFile must exist.Start
'w+'Write+ReadWipes file clean first.Start
'a+'Append+ReadCreates if missing.End

Pro-Tip: Always use the with statement. It automatically closes the file even if an error occurs.

with open(“data.txt”, “r”) as f:


5. Text File Operations

Text files store data as characters (ASCII/Unicode). Python handles the translation of \n (newline) automatically.

Reading Methods

  1. read(n): Reads \(n \) bytes. If \(n \) is empty, reads the whole file.
  2. readline(): Reads a single line (up to \n). Returns empty string '' at EOF.
  3. readlines(): Returns a List containing all lines. ['Line1\n', 'Line2\n'].

Writing Methods

  1. write(str): Writes a string. (You must add \n manually).
  2. writelines(list): Writes a list of strings. (Does not add \n automatically).

6. Binary File Operations

(Syllabus Topic: Pickle module, dump, load, EOFError)

Binary files (.dat, .jpg, .mp3) store data as raw bytes. They are faster and secure (not human readable).

Pickling (Serialization)

The process of converting a Python object (List/Dict) into a byte stream.

  • Module: import pickle
  • Function: pickle.dump(data, file_object)

Unpickling (Deserialization)

Converting a byte stream back to a Python object.

  • Function: pickle.load(file_object)

Handling the End of File (EOF):

When reading binary files, you don’t get an empty string at the end; you get an error. You must handle it with try-except.

Python

try:
    while True:
        data = pickle.load(f)
        print(data)
except EOFError:
    f.close()

7. CSV File Operations

CSV (Comma Separated Values) is the universal format for spreadsheets.

Writing CSV

We use csv.writer or csv.DictWriter.

  • Important: Open file with newline='' to prevent blank rows in Windows.

Python

import csv
with open('student.csv', 'w', newline='') as f:
    w = csv.writer(f)
    w.writerow(['Name', 'Roll'])  # Header
    w.writerow(['Ravi', 1])       # Data

Reading CSV

We use csv.reader. It returns an iterator.

Python

with open('student.csv', 'r') as f:
    data = csv.reader(f)
    for row in data:
        print(row) # row is a list ['Ravi', '1']

8. Seek and Tell: Navigating the File

(Syllabus Topic: File pointers, random access)

Imagine a cassette tape. You can rewind or fast-forward.

  • f.tell(): Tells you the current byte position (Integer).
  • f.seek(offset, whence): Moves the pointer.
    • seek(10, 0): Move 10 bytes from Beginning (0).
    • seek(10, 1): Move 10 bytes from Current (1). (Binary mode only)
    • seek(-10, 2): Move 10 bytes back from End (2). (Binary mode only)

Exam Corner: 5 Tricky MCQs

  1. Q: What is the return type of f.readlines()?
    • A) String
    • B) List
    • C) Tuple
    • Ans: B (List).
  2. Q: Which mode opens a file for reading in binary format?
    • A) rb
    • B) r
    • C) bin
    • Ans: A (rb).
  3. Q: In seek(offset, whence), what is the default value of whence?
    • A) 1 (Current)
    • B) 2 (End)
    • C) 0 (Beginning)
    • Ans: C (0).
  4. Q: Which exception is raised when pickle.load() hits the end of the file?
    • A) StopIteration
    • B) EOFError
    • C) IOError
    • Ans: B.
  5. Q: Can a global variable be modified inside a function without the global keyword?
    • A) Yes
    • B) No
    • Ans: B. (You can read it, but you cannot modify it without the keyword).

Next Lesson: [Module 4: Data Handling with Pandas]

SRIRAM
SRIRAM

Sriram is a seasoned Computer Science educator and mentor. He is UGC NET Qualified twice (2014 & 2019) and holds State Eligibility Test (SET) qualifications for both Andhra Pradesh (AP) and Telangana (TG). With years of experience teaching programming languages, he simplifies complex CS concepts for aspirants of UGC NET Computer Science, KVS, NVS, EMRS, and other competitive exams.

Leave a Reply

Your email address will not be published. Required fields are marked *