[go: up one dir, main page]

0% found this document useful (0 votes)
2 views16 pages

UNIT 5 notes

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 16

UNIT 5

FILES, MODULES AND PACKAGES


File Handling: Introduction, with Statement, The seek() and Tell() methods, Testing the existence of a file,
Handling binary data, Handling CSV files, modules, packages-numpy, pandas, Matplotlib. Illustrative
programs: word count, copy file, Voter's age validation, Creating user defined exception.
File Handling:
 Python supports file handling and allows users to handle files i.e., to read and write files, along with
many other file handling options, to operate on files.
 The concept of file handling has stretched over various other languages, but the implementation is
either complicated or lengthy, like other concepts of Python, this concept here is also easy and short.
 Python treats files differently as text or binary and this is important. Each line of code includes a
sequence of characters, and they form a text file. Each line of a file is terminated with a special
character, called the EOL or End of Line characters like comma {,} or newline character. It ends the
current line and tells the interpreter a new one has begun. Let’s start with the reading and writing
files.
Advantages: File Handling
 Versatility: File handling in Python allows you to perform a wide range of operations, such as
creating, reading, writing, appending, renaming, and deleting files.
 Flexibility: File handling in Python is highly flexible, as it allows you to work with different file
types (e.g. text files, binary files, CSV files , etc.), and to perform different operations on files (e.g.
read, write, append, etc.).
 User – friendly: Python provides a user-friendly interface for file handling, making it easy to create,
read, and manipulate files.
 Cross-platform: Python file-handling functions work across different platforms (e.g. Windows, Mac,
Linux), allowing for seamless integration and compatibility.
Disadvantages: File Handling
 Error-prone: File handling operations in Python can be prone to errors, especially if the code is not
carefully written or if there are issues with the file system (e.g. file permissions, file locks, etc.).
 Security risks: File handling in Python can also pose security risks, especially if the program
accepts user input that can be used to access or modify sensitive files on the system.
 Complexity: File handling in Python can be complex, especially when working with more advanced
file formats or operations. Careful attention must be paid to the code to ensure that files are handled
properly and securely.
 Performance: File handling operations in Python can be slower than other programming
languages, especially when dealing with large files or performing complex operations.
File:
 File is a named location on disk to store related information. It is used to permanently store data in
a memory (e.g. hard disk).
File Types:
1. Text file
2. Binary file
Operations on Files:
In Python, a file operation takes place in the following order,
1. Opening a file
2. Reading / Writing file
3. Closing the file

Python File Open:


 Python File Open
 Before performing any operation on the file like reading or writing, first, we have to open that file.
For this, we should use Python’s inbuilt function open() but at the time of opening, we have to
specify the mode, which represents the purpose of the opening file.
 f = open(filename, mode)
Where the following mode is supported:
 r: open an existing file for a read operation.
 w: open an existing file for a write operation. If the file already contains some data, then it will be
overridden but if the file is not present then it creates the file as well.
 a: open an existing file for append operation. It won’t override existing data.
 r+: To read and write data into the file. This mode does not override the existing data, but you can
modify the data starting from the beginning of the file.
 w+: To write and read data. It overwrites the previous file if one exists, it will truncate the file to
zero length or create a file if it does not exist.
 a+: To append and read data from the file. It won’t override existing data.
Example 1: The open command will open the Python file in the read mode and the for loop will print each
line present in the file.
# a file named “python", will be opened with the reading mode.
file = open(‘python.txt', 'r')
# This will print every line one by one in the file
for each in file:
print (each)
With Statement:
 In Python, with statement is used in exception handling to make the code cleaner and much more
readable.
 It simplifies the management of common resources like file streams.
 Observe the following code example on how the use of with statement makes code cleaner.
 The with statement is used when you are working with resources that need to be managed, such as
files or database connections.
 It ensures that these resources are properly initialized before your code executes and automatically
cleaned up afterwards, regardless of whether the code runs successfully or raises an exception.
 This makes it particularly useful for handling resource-intensive operations where proper cleanup
is crucial.
 The primary advantage of using the with statement is automatic resource management. It
eliminates the need for explicit try and finally blocks to ensure proper cleanup of resources.
 When the with block exits, it automatically calls the __exit__() method of the context manager,
ensuring that resources are released or cleaned up appropriately.
# file handling
# 1) without using with statement
file = open('file_path', 'w')
file.write('hello world !')
file.close()
# 2) without using with statement
file = open('file_path', 'w')
try:
file.write('hello world')
finally:
file.close()
# using with statement
with open('file_path', 'w') as file:
file.write('hello world !')
The seek():
 The seek() function in Python is used to move the file cursor to the specified location.
 When we read a file, the cursor starts at the beginning, but we can move it to a specific position by
passing an arbitrary integer (based on the length of the content in the file) to the seek() function.
Syntax:
file_object.seek(offset,whence)
Here,
offset – Required parameter. Sets the cursor to the specified position and starts reading after that position.
whence – Optional parameter. It is used to set the point of reference to start from which place.
0 – Default. Sets the point of reference at the beginning of the file. Equivalent to os.SEEK_SET.
1 – Sets the point of reference at the current position of the file. Equivalent to os.SEEK_CUR.
2 – Sets the point of reference at the end of the file. Equivalent to os.SEEK_END.

Example:
import os

# Creates a text file with Example text


with open('demofile.txt', 'w') as f:
# Adding Example Text
f.write('This text is an example.')

f = open('demofile.txt', 'r')

# Prints first line of document from default position


print(f.readline())

# Changes the reference point


f.seek(5)

# Prints the first line from new reference point


print(f.readline())

# Closes the file


f.close()

OUTPUT:
This text is an example.
text is an example.
Tell():
 The seek() function is used to set the position of the file cursor, whereas the tell() function returns
the position where the cursor is set to begin reading.
Syntax:
tell()
 The tell() function takes no parameter.

Example:
# Open a file
fo = open("demo.txt", "w")
print("Name of the file: ", fo.name)
# Write into the file using write() method
fo.write("This is a demo file")
# Get the current position of the file.
pos = fo.tell()
print("Current Position:", pos)
# Close opened file
fo.close()
Output:
Name of the file: demo.txt
Current Position: 19
Handling Binary Data:
Alright, lets get this out of the way! The basics are pretty standard:
1. There are 8 bits in a byte
2. Bits either consist of a 0 or a 1
3. A byte can be interpreted in different ways, like binary octal or hexadecimal
Note: These are not character encodings, those come later. This is just a way to look at a set of 1’s and 0’s
and see it in three different ways(or number systems).
Examples:
Input: 10011011
Output:
1001 1011 ---- 9B (in hex)
1001 1011 ---- 155 (in decimal)
1001 1011 ---- 233 (in octal)
 Now that we know what a byte is and what it looks like, let us see how it is interpreted, mainly
in strings.
 Character Encodings are a way to assign values to bytes or sets of bytes that represent a certain
character in that scheme.
 Some encodings are ASCII(probably the oldest), Latin, and UTF-8(most widely used as of today. In a
sense encodings are a way for computers to represent, send and interpret human readable
characters.
 This means that a sentence in one encoding might become completely incomprehensible in another
encoding.
Python and Bytes
 From a developer’s point of view, the largest change in Python 3 is the handling of strings.
 In Python 2, the str type was used for two different kinds of values – text and bytes, whereas in
Python 3, these are separate and incompatible types.
 This means that before Python3 we could treat a set of bytes as a string and work from there, this
is not the case now, now we have a separate data type, called bytes.
 This data type can be briefly explained as a string of bytes, which essentially means, once the
bytes data type is initialized it is immutable.
Example:
bytesArr = bytearray(b'\x00\x0F')
# Bytearray allows modification
bytesArr[0] = 255
bytesArr.append(255)
print(bytesArr)
Output:
bytearray(b'\xff\x0f\xff')
Bitwise Operations
 In Python, bitwise operators are used to perform bitwise calculations on integers. The integers are
first converted into binary and then operations are performed on bit by bit, hence the name bitwise
operators. The standard bitwise operations are demonstrated below.
Example:
# Code to demonstrate bitwise operations
# Some bytes to play with
byte1 = int('11110000', 2) # 240
byte2 = int('00001111', 2) # 15
byte3 = int('01010101', 2) # 85
# Ones Complement (Flip the bits)
print(~byte1)
# AND
print(byte1 & byte2)
# OR
print(byte1 | byte2)
# XOR
print(byte1 ^ byte3)
# Shifting right will lose the
# right-most bit
print(byte2 >> 3)
# Shifting left will add a 0 bit
# on the right side
print(byte2 << 1)
# See if a single bit is set
bit_mask = int('00000001', 2) # Bit 1
# Is bit set in byte1?
print(bit_mask & byte1)
# Is bit set in byte2?
print(bit_mask & byte2)
Output:
-241
0
255
165
1
30
0
1
Handling CSV Files:
 CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a
spreadsheet or database.
 A CSV file stores tabular data (numbers and text) in plain text.
 Each line of the file is a data record. Each record consists of one or more fields, separated by
commas.
 The use of the comma as a field separator is the source of the name for this file format. For working
CSV files in Python, there is an inbuilt module called CSV.
Working with CSV files in Python:
 Reading a CSV file
 Reading CSV Files Into a Dictionary With csv
 Writing to a CSV file
 Writing a dictionary to a CSV file
 Reading CSV Files With Pandas
 Writing CSV Files With Pandas
 Storing email in CSV file
Reading a CSV file:

 Reading from a CSV file is done using the reader object. The CSV file is opened as a text file with
Python’s built-in open() function, which returns a file object.
 In this example, we first open the CSV file in READ mode, file object is converted to csv.reader object
and further operation takes place. Code and detailed explanation is given below.
# importing csv module
import csv
# csv file name
filename = "aapl.csv"
# initializing the titles and rows list
fields = []
rows = []
# reading csv file
with open(filename, 'r') as csvfile:
# creating a csv reader object
csvreader = csv.reader(csvfile)
# extracting field names through first row
fields = next(csvreader)
# extracting each data row one by one
for row in csvreader:
rows.append(row)
# get total number of rows
print("Total no. of rows: %d" % (csvreader.line_num))

# printing the field names


print('Field names are:' + ', '.join(field for field in fields))
# printing first 5 rows
print('\nFirst 5 rows are:\n')
for row in rows[:5]:
# parsing each column of a row
for col in row:
print("%10s" % col, end=" "),
print('\n')
Output:
The above example uses a CSV file aapl.csv which can be downloaded from here .
Run this program with the aapl.csv file in the same directory.
 Let us try to understand this piece of code.
with open(filename, 'r') as csvfile:
csvreader = csv.reader(csvfile)
 Here, we first open the CSV file in READ mode. The file object is named as csvfile . The file object is
converted to csv.reader object. We save the csv.reader object as csvreader.
fields = csvreader.next()
 csvreader is an iterable object. Hence, .next() method returns the current row and advances the
iterator to the next row. Since, the first row of our csv file contains the headers (or field names), we
save them in a list called fields.
for row in csvreader:
rows.append(row)
 Now, we iterate through the remaining rows using a for loop. Each row is appended to a list
called rows . If you try to print each row, one can find that a row is nothing but a list containing all the
field values.
print("Total no. of rows: %d"%(csvreader.line_num))
 csvreader.line_num is nothing but a counter which returns the number of rows that have been
iterated.
Reading CSV Files Into a Dictionary With CSV:
We can read a CSV file into a dictionary using the csv module in Python and the csv.DictReader class.
Here’s an example:

Suppose, we have a employees.csv file and content inside it will be:

name,department,birthday_month
John Smith,HR,July
Alice Johnson,IT,October
Bob Williams,Finance,January
Writing to a CSV file:
To write to a CSV file, we first open the CSV file in WRITE mode. The file object is converted to
csv.writer object and further operations takes place. Code and detailed explanation is given below.
# importing the csv module
import csv
# field names
fields = ['Name', 'Branch', 'Year', 'CGPA']
# data rows of csv file
rows = [['Nikhil', 'COE', '2', '9.0'],
['Sanchit', 'COE', '2', '9.1'],
['Aditya', 'IT', '2', '9.3'],
['Sagar', 'SE', '1', '9.5'],
['Prateek', 'MCE', '3', '7.8'],
['Sahil', 'EP', '2', '9.1']]
# name of csv file
filename = "university_records.csv"
# writing to csv file
with open(filename, 'w') as csvfile:
# creating a csv writer object
csvwriter = csv.writer(csvfile)
# writing the fields
csvwriter.writerow(fields)
# writing the data rows
csvwriter.writerows(rows)
Let us try to understand the above code in pieces.
 fields and rows have been already defined. fields is a list containing all the field names. rows is a list
of lists. Each row is a list containing the field values of that row.
with open(filename, 'w') as csvfile:
csvwriter = csv.writer(csvfile)
 Here, we first open the CSV file in WRITE mode. The file object is named as csvfile . The file object is
converted to csv.writer object. We save the csv.writer object as csvwriter .
csvwriter.writerow(fields)
 Now we use writerow method to write the first row which is nothing but the field names.
csvwriter.writerows(rows)
 We use writerows method to write multiple rows at once.
Modules:
A Python module is a file containing Python definitions and statements. A module can define functions,
classes, and variables. A module can also include runnable code.
Create a Python Module:
# A simple module, calc.py
def add(x, y):
return (x+y)
def subtract(x, y):
return (x-y)
Import module in Python:
Import the functions, and classes defined in a module to another module using the import
statement in some other Python source file.
When the interpreter encounters an import statement, it imports the module if the module is present in the
search path.
Syntax to Import Module in Python
import module
Importing modules in Python Example:
# importing module calc.py
import calc
print(calc.add(10, 2))
Python Import From Module:
# importing sqrt() and factorial from the
# module math
from math import sqrt, factorial
# if we simply do "import math", then
# math.sqrt(16) and math.factorial()
# are required.
print(sqrt(16))
print(factorial(6))
Output:
4.0
720
Import all Names:
The * symbol used with the import statement is used to import all the names from a module to a current
namespace.
Syntax:
from module_name import *
Example:
# importing sqrt() and factorial from the
# module math
from math import *
# if we simply do "import math", then
# math.sqrt(16) and math.factorial()
# are required.
print(sqrt(16))
print(factorial(6))
Output:
4.0
720
Locating Python Modules:
 First, it searches for the module in the current directory.
 If the module isn’t found in the current directory, Python then searches each directory in the shell
variable PYTHONPATH. The PYTHONPATH is an environment variable, consisting of a list of
directories.
 If that also fails python checks the installation-dependent list of directories configured at the time
Python is installed.

Directories List for Modules:


# importing sys module
import sys
# importing sys.path
print(sys.path)
Output:
[‘/home/nikhil/Desktop/gfg’, ‘/usr/lib/python38.zip’, ‘/usr/lib/python3.8’, ‘/usr/lib/python3.8/lib-
dynload’, ”, ‘/home/nikhil/.local/lib/python3.8/site-packages’, ‘/usr/local/lib/python3.8/dist-packages’,
‘/usr/lib/python3/dist-packages’, ‘/usr/local/lib/python3.8/dist-packages/IPython/extensions’,
‘/home/nikhil/.ipython’]
Renaming the Python Module:
Syntax: Import Module_name as Alias_name
Example:
# importing sqrt() and factorial from the
# module math
import math as mt
# if we simply do "import math", then
# math.sqrt(16) and math.factorial()
# are required.
print(mt.sqrt(16))
print(mt.factorial(6))
Output:
4.0
720
Python Built-in modules:
# importing built-in module math
import math

# using square root(sqrt) function contained


# in math module
print(math.sqrt(25))

# using pi function contained in math module


print(math.pi)

# 2 radians = 114.59 degrees


print(math.degrees(2))

# 60 degrees = 1.04 radians


print(math.radians(60))

# Sine of 2 radians
print(math.sin(2))

# Cosine of 0.5 radians


print(math.cos(0.5))

# Tangent of 0.23 radians


print(math.tan(0.23))

# 1 * 2 * 3 * 4 = 24
print(math.factorial(4))

# importing built in module random


import random

# printing random integer between 0 and 5


print(random.randint(0, 5))

# print random floating point number between 0 and 1


print(random.random())

# random number between 0 and 100


print(random.random() * 100)

List = [1, 4, True, 800, "python", 27, "hello"]

# using choice function in random module for choosing


# a random element from a set such as a list
print(random.choice(List))

# importing built in module datetime


import datetime
from datetime import date
import time

# Returns the number of seconds since the


# Unix Epoch, January 1st 1970
print(time.time())

# Converts a number of seconds to a date object


print(date.fromtimestamp(454554))
Output:
5.0
3.14159265359
114.591559026
1.0471975512
0.909297426826
0.87758256189
0.234143362351
24
3
0.401533172951
88.4917616788
True
1461425771.87

Packages:
 Python Packages are a way to organize and structure your Python code into reusable components.
Think of it like a folder that contains related Python files (modules) that work together to provide
certain functionality.
 Packages help keep your code organized, make it easier to manage and maintain, and allow you to
share your code with others.
 They’re like a toolbox where you can store and organize your tools (functions and classes) for
easy access and reuse in different projects.
How to Create Package in Python?
Creating packages in Python allows you to organize your code into reusable and manageable modules.
Here’s a brief overview of how to create packages:
 Create a Directory: Start by creating a directory (folder) for your package. This directory will
serve as the root of your package structure.
 Add Modules: Within the package directory, you can add Python files (modules) containing your
code. Each module should represent a distinct functionality or component of your package.
 Init File: Include an __init__.py file in the package directory. This file can be empty or can contain
an initialization code for your package. It signals to Python that the directory should be treated as
a package.
 Subpackages: You can create sub-packages within your package by adding additional directories
containing modules, along with their own __init__.py files.
 Importing: To use modules from your package, import them into your Python scripts using dot
notation. For example, if you have a module named module1.py inside a package named
mypackage, you would import its function like this: from mypackage.module1 import greet.
 Distribution: If you want to distribute your package for others to use, you can create a setup.py
file using Python’s setuptools library. This file defines metadata about your package and specifies
how it should be installed.
Code Example
Here’s a basic code sample demonstrating how to create a simple Python package:
1. Create a directory named mypackage.
2. Inside mypackage, create two Python files: module1.py and module2.py.
3. Create an __init__.py file inside mypackage (it can be empty).
4. Add some code to the modules.
5. Finally, demonstrate how to import and use the modules from the package.
mypackage/

├── __init__.py
├── module1.py
└── module2.py
Python Numpy:
Numpy is a general-purpose array-processing package. It provides a high-performance
multidimensional array object, and tools for working with these arrays. It is the fundamental package for
scientific computing with Python.
Arrays in Numpy
Array in Numpy is a table of elements (usually numbers), all of the same type, indexed by a tuple of
positive integers. In Numpy, number of dimensions of the array is called rank of the array.A tuple of
integers giving the size of the array along each dimension is known as shape of the array. An array class
in Numpy is called as ndarray. Elements in Numpy arrays are accessed by using square brackets and can
be initialized by using nested Python Lists.
Creating a Numpy Array
Arrays in Numpy can be created by multiple ways, with various number of Ranks, defining the size of the
Array. Arrays can also be created with the use of various data types such as lists, tuples, etc. The type of
the resultant array is deduced from the type of the elements in the sequences.
Example:
# Python program for
# Creation of Arrays
import numpy as np

# Creating a rank 1 Array


arr = np.array([1, 2, 3])
print("Array with Rank 1: \n",arr)

# Creating a rank 2 Array


arr = np.array([[1, 2, 3],
[4, 5, 6]])
print("Array with Rank 2: \n", arr)
# Creating an array from tuple
arr = np.array((1, 3, 2))
print("\nArray created using "
"passed tuple:\n", arr)
Output:
Array with Rank 1:
[1 2 3]
Array with Rank 2:
[[1 2 3]
[4 5 6]]

Array created using passed tuple:


[1 3 2]
Data Types in Numpy
 Every Numpy array is a table of elements (usually numbers), all of the same type, indexed by a
tuple of positive integers.
 Every ndarray has an associated data type (dtype) object. This data type object (dtype) provides
information about the layout of the array. The values of an ndarray are stored in a buffer which
can be thought of as a contiguous block of memory bytes which can be interpreted by the dtype
object.
 Numpy provides a large set of numeric data types that can be used to construct arrays. At the time
of Array creation, Numpy tries to guess a data type, but functions that construct arrays usually
also include an optional argument to explicitly specify the data type.

Pandas:
 Pandas is a Python library used for working with data sets.
 It has functions for analyzing, cleaning, exploring, and manipulating data.
 Pandas allows us to analyze big data and make conclusions based on statistical theories.
 Pandas can clean messy data sets, and make them readable and relevant.
Example Program:
import pandas as pd

df = pd.read_csv('data.csv')

print(df.to_string())
Output:

Matplotlib:

Matplotlib is a low level graph plotting library in python that serves as a visualization utility.
Matplotlib was created by John D. Hunter.
Matplotlib is open source and we can use it freely.
Matplotlib is mostly written in python, a few segments are written in C, Objective-C and Javascript for
Platform compatibility.

You might also like