File Handling in Python

A. Reading from and Writing to Files:

Reading from Files (open() and read()):

  • To read from a file, you can use the open() function in Python, which opens a file and returns a file object. The read() method is used to read the contents of the file.
  • Syntax for Reading:

python

# Reading from a file file = open(‘file.txt’, ‘r’) # Opens the file in read mode (‘r’) content = file.read() # Reads the entire file content print(content) file.close() # Close the file after reading

Writing to Files (open() and write()):

  • To write to a file, open it with the appropriate mode (‘w’ for write, ‘a’ for append). The write() method is used to write content to the file.
  • Syntax for Writing:

python

# Writing to a file file = open(‘file.txt’, ‘w’) # Opens the file in write mode (‘w’) file.write(‘Hello, World!\n’) # Writes content to the file file.close() # Close the file after writing

B. File Modes and Operations:

File Modes:

  • Read Mode (‘r’): Opens a file for reading. Raises an error if the file does not exist.
  • Write Mode (‘w’): Opens a file for writing. Creates a new file if it doesn’t exist or truncates the file if it exists.
  • Append Mode (‘a’): Opens a file for appending new content. Creates a new file if it doesn’t exist.
  • Read and Write Mode (‘r+’): Opens a file for both reading and writing.
  • Binary Mode (‘b’): Used in conjunction with other modes (e.g., ‘rb’, ‘wb’) to handle binary files.

File Operations:

  • read(): Reads the entire content of the file or a specified number of bytes.
  • readline(): Reads a single line from the file.
  • readlines(): Reads all the lines of a file and returns a list.
  • write(): Writes content to the file.
  • close(): Closes the file when finished with file operations.

Using with Statement (Context Manager):

  • The with statement in Python is used to automatically close the file when the block of code is exited. It’s a good practice to use it to ensure proper file handling.
  • Syntax:

python

with open(‘file.txt’, ‘r’) as file: content = file.read() print(content) # File is automatically closed outside the ‘with’ block

VII. Object-Oriented Programming (OOP) Basics

A. Classes and Objects:

Classes:

  • Classes are blueprints for creating objects in Python. They encapsulate data (attributes) and behaviors (methods) into a single unit.
  • Syntax for Class Declaration:

python

# Class declaration class MyClass: # Class constructor (initializer) def __init__(self, attribute1, attribute2): self.attribute1 = attribute1 self.attribute2 = attribute2 # Class method def my_method(self): return “This is a method in MyClass”

Objects:

  • Objects are instances of classes. They represent real-world entities and have attributes and behaviors defined by the class.
  • Creating Objects from a Class:

python

# Creating an object of MyClass obj = MyClass(“value1”, “value2”)

B. Inheritance and Polymorphism:

Inheritance:

  • Inheritance allows a class (subclass/child class) to inherit attributes and methods from another class (superclass/parent class).
  • Syntax for Inheritance:

python

# Parent class class Animal: def sound(self): return “Some sound” # Child class inheriting from Animal class Dog(Animal): def sound(self): # Overriding the method return “Woof!”

Polymorphism:

  • Polymorphism allows objects of different classes to be treated as objects of a common superclass. It enables the same method name to behave differently for each class.
  • Example of Polymorphism:

python

# Polymorphism example def animal_sound(animal): return animal.sound() # Same method name, different behaviors # Creating instances of classes animal1 = Animal() dog = Dog() # Calling the function with different objects print(animal_sound(animal1)) # Output: “Some sound” print(animal_sound(dog)) # Output: “Woof!”