PSPP Unit-5
PSPP Unit-5
GE3151
PROBLEM SOLVING AND
PYTHON PROGRAMMING
B.TECH
I YEAR – I SEM
(2024-2025)
UNIT – 5
FILES,MODULES,PACKAGES
Presented
By
Asst.prof.A.Charmil
a
UNIT – V
FILES,MODULES,PACKAGES
FILES
File Operation:
Open a file
Reading a file
Writing a file
Closing a file
1.Open ( ) function:
Python built in open function to get a file object.
The open function opens a file.
It returns a something called a file object.
File objects can turn methods and attributes that can be
used to collect.
Syntax:
file_object=open(“file_name”,”mode”)
Example:
fp=open=open(“text.txt”,”r”)
Create a text file:
fp=open(“text.txt”,”w”)
2.Read ( ) Function:
The different methods of read functions are,
Read( ) - return one big string
Readline( ) - return one line at a time
Readlines( ) - return a list of lines
Read line-by-line – return iterating in for loop
Syntax:
file_name.read ( )
Example:
fp=open(“a.txt”,”w”)
Print(fp.read())
Print(fp.read(6))
Print(fp.readline())
Print(fp.readline(3))
Print(fp.read())
Output:
a.txt
Coding:
Source=open(“a.txt”,”r”)
destination=open(“b.txt”,”w”)
for line in source:
destination.write(line)
source.close()
destination.close()
Output:
Input a.txt Output b.txt
A file stores data, A file stores data, information,
information,settings or settings or commands in
commands in secondary secondary storage device like
storage device like magnetic magnetic disk, magnetic tape,
disk, magnetic tape, optical optical disk, flash memory
disk, flash memory
Coding:
fp=open(“a.txt”,”r”)
Line=0
Word=0
Character=0
For line in fp:
Words=line.split( )
Line=line+1
Word=word+len(words)
Character=Character+len(line)
print(“Number of line”,line)
print(“Number of words”,word)
print(“Number of character”,Character)
output:
Number
Numberofofline=5
line=5
Number
Numberofofwords=15
Words=15
Number
Number of of character=47
Characters=47
Types of errors:
1. Syntax error or compile time error
2. Run time error
3. Logical error
1).Syntax errors:
Syntax errors are the errors which are displayed when the
programmer do mistakes when writing a a program, when
a program has syntax error it will get executed .
Leaving out a keyword
Leaving out a symbol, such as colon, comma,
brackets
Misspelling a keyword
Incorrect identation
2).Runtime errors:
If a programmar is syntatically correct-that is free of
syntax errors-it will be run by python interpreter
However, the program may execute unexpectedly during
execution if it encounters a runtime error
when a program has runtime error it will get executed but
it will not produce output
Division by zero
performing an operation on incompatible types
Using an identifier which has not been defined
Trying to access a file which doesn’t exit
3).Logical errors:
Logical errors are the most difficult to fix
They occur when the program runs without crashing but
produces incorrect result
Using the wrong variable name
Indenting a blocks to wrong level
Using integer division instead of floating point
division
Getting operator precendence wrong
II - EXCEPTIONS
Execptions:
An exception is an error thet happends during execution of
a program. When that error occurs.
Exception Handling Mechanism:
1.try-except
2.try-multiple except
3.try-except-else
4.raise exception
5.try-except-finally
The try and Exception type The else part The raise A finally clause
except must be will be statement is always
statements are different for excecuted only allows the executed
used to handle except if the try block programmer to before leaving
runtime errors. statements. does not raise force a the try
the exception. specified statement,
exception to whether an
Output:1 Output:1 Output:1 Output: Output :1
Enter the value of Enter the value of Enter the value of An exception flew Enter the value of
X=10 X=10 X=10 by! Traceback X=10
Enter the value of Enter the value of Enter the value of (most recent call Enter the value of
Y=5 Y=5 Y=5 last): Y=5
sum of 10 and 5 = result = 2 File”<stdin>”,line Result = 2
15 1, in <module> excecuting finally
Division of 10 and NameError:HiThe clause
5=2 re
MODULES
Modules In Python:
A python module is a file that consists of python definition
and statements. A module can define functions, classes
and variables.
1.Import statement
Syntax:
import module 1, [module 2,[….module]]
Example:
>>>import math
>>>print 9math.pi)
3.14159265
2.Import with renaming:
Example:
>>>import math as a
>>>print(“The value of pi is”,a.pi)
The value of pi is 3.14159265
3.Import file name:
Coding:
import support
support.add (3,4)
support.add (3.5,4.7)
support.add (‘a’,’b’)
support.add (“yona”,”alex”)
support.add (‘fleming’)
Output:
The result is 7
The result is 8.2
The result is ab
The result is yonaalex
Welcome,fleming
Output:
The result is 7
The result is 8.2
The result is ab
The result is yonaalex
Welcome,fleming
5.OS Module:
The OS module in python provide function for
interacting with operating sysytem.
6.Sys Module:
Sys module provides information about constant,
function and methods.
It provides acess to some variables used or
maintained by the interpreter.
Steps to create the own module:
Here we are going to create a calc module; our module
contains four functions
PACKAGES
PACKAGES IN PYTHON:
A Pacakage is a way of collecting related modules together
within a single treee like hierarchy.
It has a well organized hierarchy for easier access.
The directory can contain sub direction and files were as a
python package can have sub packages and modules.
A package must contain a file named_init_.py in order for
python to consider it as a package.
This file can be left empty.
EX:
STEPS TO CREATE A PYTHON PACKAGE: