Python Unit-5
Python Unit-5
· Types of File
· What is Exception?
· Regular expressions
Types of Files
● Binary files: These contain raw data in binary format (e.g., .jpg, .mp3,
.exe).
○ Binary files store data in the form of bytes, and this data may not
be human-readable.
File Objects
When a file is opened in Python using the built-in open() function, a file object
is returned. The file object allows us to interact with the file, read from it, write
to it, or perform other operations.
● Here, file is the file object used to interact with the file example.txt.
Python provides several built-in functions to open, close, and manage files.
Here are some of the most commonly used functions:
● close(): Closes the file object. It’s important to close a file when you're
done working with it to free up system resources.
● flush(): Flushes the internal buffer to the file. It ensures that any data still
in memory gets written to the file.
● seek(offset, whence): Moves the file pointer to a specific location.
whence can be 0 (absolute file positioning), 1 (relative to current
position), or 2 (relative to file's end).
● tell(): Returns the current position of the file pointer.
File Built-in Methods
File objects have several built-in methods that can be used to perform
operations on the file. These methods provide a convenient way to interact with
files.
1. read(size=-1): Reads the entire file (or a specified number of bytes if size
is provided).
2. readline(size=-1): Reads one line from the file. If size is specified, it will
read up to that number of bytes.
3. readlines(hint=-1): Reads the file and returns a list of lines. The hint
parameter specifies the number of bytes to read.
4. write(string): Writes the string to the file. If the file is in write or append
mode, it will overwrite or append the text respectively.
In Python, reading from and writing to files are common operations. These
operations are done using the open() function and various associated methods
for file objects.
2. Opening a File
Before performing any read/write operation, you need to open a file using the
open() function.
Python provides several methods to read from a file. These methods can be used
depending on how you want to handle the data.
read() Method:
● The read() method reads the entire content of the file.
● Syntax: file.read(size)
○ If no size is provided, it reads the entire file.
○ If size is specified, it reads that number of characters (or bytes in
binary mode).
readline() Method:
● The readlines() method reads all lines in the file and returns them as a list
of strings.
● Each element in the list represents a line from the file, including the
newline character \n.
You can write data to a file using the write() or writelines() method.
NOTE:
write() Method:
It is a good practice to use the with statement when working with files. It
ensures that the file is properly closed after the block of code is executed, even
if an exception occurs.
The with statement automatically handles the opening and closing of the file,
reducing the risk of leaving files open accidentally.
The file cursor (pointer) refers to the current position in the file where the next
read or write operation will happen. You can move the cursor to a specific
position using the seek() method and check its current position using the tell()
method.
seek() Method:
RamShyamJiaRohanRia
fw.seek(3): ShyamJiaRohanRia
fw.seek(1,0): amShyamJiaRohanRia
fw.seek(11,0): RohanRia
fw.seek(0,1) #while using current position we cannot use non-zero
value as offset
fw.seek(3,1) #non-zero value works with binary mode
fw.seek(-5,2): anRIA #non-zero value works with binary mode
tell() Method:
● The tell() method returns the current position of the file pointer.
f1 = open(“C:\pictures\sea.jpg”,”rb”)
Errors in Python : Compile-Time Errors ,Runtime Errors , Logical Errors
1. Compile-Time Errors:
Definition:
● These are errors that occur when the Python code is being converted into
bytecode during the compilation phase.
● Python is an interpreted language, but before execution, it undergoes a
compilation step to convert the source code to bytecode. If there’s a
syntax issue, the interpreter can’t complete the compilation process.
Common Causes:
● Syntax Errors: When Python can't understand the code due to incorrect
syntax.
○ Examples:
■ Misspelled keywords: print(), for, if
■ Missing parentheses or braces
■ Unclosed quotes
How to Identify: These errors are usually caught before the program is
executed. If there is a syntax issue, Python will throw a SyntaxError at
the time of compilation.
Example:
Output:
2. Logical Errors:
Definition:
● These errors occur when the program runs without crashing, but it
produces incorrect or unexpected results due to a flaw in the logic of the
code.
● The code runs without raising any exceptions, but the output is not what
was intended, making it harder to detect.
Common Causes:
● Incorrect calculations
● Mistaken conditions in loops or if statements
● Wrong algorithm implementation
● Misuse of variables or functions
How to Identify: Logical errors do not cause the program to crash, but
they lead to incorrect results. Identifying these errors often requires
reviewing the program logic, output, and verifying against expected
behavior.
def find_max(numbers):
largest = 0 # Logical Error: what if all numbers are negative?
for num in numbers:
if num > largest:
largest = num
return largest
3. Runtime Errors:
Definition:
● These errors occur during the execution of the program, after it has been
compiled successfully.
● The program runs, but an issue arises due to invalid operations, or
something goes wrong while executing the code.
Common Causes:
How to Identify: These errors occur when the program is running and
cause the program to stop unexpectedly. Python typically raises
exceptions (e.g., ZeroDivisionError, IndexError, TypeError) to handle
such runtime errors.
Examples:
# ZeroDivisionError
x = 5 / 0 # Error: division by zero
Output:
Example:
# IndexError
my_list = [1, 2, 3]
print(my_list[5]) # Error: list index out of range
Output:
Summary of Errors:
Type Description Example
What is an Exception?
Types of Exceptions:
Built-In Exceptions
Example:
try:
num = int(input("Enter a number: "))
result = 10 / num
print(result)
except ZeroDivisionError:
print("Cannot divide by zero!")
except ValueError:
print("Invalid input! Please enter a valid number.")
try...except...else
Syntax:
try:
# Code that may raise an exception
except ExceptionType:
# Code to handle the exception
else:
# Code to execute if no exception occurs
How it works:
● If the code inside the try block runs without an error, the else block will
execute.
● If an error occurs inside the try block, Python will jump to the appropriate
except block.
Example:
try:
num = int(input("Enter a number: "))
print(10 / num)
except ZeroDivisionError:
print("You can't divide by zero!")
else:
print("Division successful!")
● If no error occurs in the try block, the message is printed in the else
block.
● If there's an error in the try block (e.g., ValueError or
ZeroDivisionError), the program will print an error message from
the except block.
try...finally
Syntax:
try:
# Code that may raise an exception
finally:
# Code that will always execute, even if an exception occurs
How it works:
Example:
try:
file = open("sample.txt", "r")
data = file.read()
except FileNotFoundError:
print("The file does not exist.")
finally:
file.close() # Ensures the file is closed even if an error occurs
print("File closed.")
In this example:
Output:
Enter rollno : 0
Enter name : John
Rollno can't be <= 0
Enter rollno : a
Some problem occured
Regular Expressions
For Example:
^a...s$
The above code defines a RegEx pattern. The pattern is: any five letter string
starting with a and ending with s.
alias Match
abyss Match
Alias No match
An abacus No match
Python's re Module
import re
Function Description
Example:
import re
pattern = '^a...s$'
test_string = 'abyss'
result = re.match(pattern, test_string)
if result:
print("Search successful.")
else:
print("Search unsuccessful.")
Quantifiers/Metacharacters
[] - Square brackets: Square brackets specifies a set of characters you wish to
match.
[abc] a 1 match
ac 2 matches
abc de ca 5 matches
You can also specify a range of characters using - inside square brackets.
You can complement (invert) the character set by using caret ^ symbol at the
start of a square-bracket.
.. a No match
ac 1 match
acd 1 match
^ - Caret: The caret symbol ^ is used to check if a string starts with a certain
character.
^a a 1 match
abc 1 match
bac No match
a$ a 1 match
formula 1 match
cab No match
* - Star: The star symbol * matches zero or more occurrences of the pattern left
to it.
ma*n mn 1 match
man 1 match
maaan 1 match
woman 1 match
+ - Plus: The plus symbol + matches one or more occurrences of the pattern left
to it.
man 1 match
maaan 1 match
woman 1 match
ma?n mn 1 match
man 1 match
woman 1 match
{} - Braces: Consider this code: {n,m}. This means at least n, and at most m
repetitions of the pattern left to it.
Let's try one more example. This RegEx [0-9]{2, 4} matches at least 2 digits
but not more than 4 digits
1 and 2 No match
(a|b|c)xz ab xz No match
If you are unsure if a character has special meaning or not, you can put \ in front
of it. This makes sure the character is not treated in a special way.
Special Sequences
a football Match
afootball No match
a football No match
afootball Match
Python No match
1345 No match
PythonRegEx No match
\S ab 2 matches (at a b)
No match
\w - Matches any alphanumeric character (digits and alphabets).
Equivalent to [a-zA-Z0-9_]. By the way, underscore _ is also considered an
alphanumeric character.
%"> ! No match
Python No match
Example:
# Program to extract numbers from a string
import re
re.split(): The re.split method splits the string where there is a match and
returns a list of strings where the splits have occurred.
Example:
import re
string = 'Twelve:12 Eighty nine:89.'
pattern = '\d+'
If the pattern is not found, re.split() returns a list containing the original
string.
You can pass maxsplit argument to the re.split() method. It's the
maximum number of splits that will occur.
import re
By the way, the default value of maxsplit is 0; meaning all possible splits.
re.sub(): The method returns a string where matched occurrences are replaced
with the content of replace variable.The syntax of re.sub() is:
re.sub(pattern, replace, string)
Example:
# Program to remove all whitespaces
import re
# multiline string
string = 'abc 12\de
23 \n f45 6'
# empty string
replace = ''
# multiline string
string = 'abc 12\de 23
f45 6'
# matches all whitespace characters
pattern = '\s+'
replace = ''
# Output:
# abc12de 23 f45 6
re.search(): The re.search() method takes two arguments: a pattern and a string.
The method looks for the first location where the RegEx pattern produces a
match with the string.
If the search is successful, re.search() returns a match object; if not, it returns
None.
Syntax: match = re.search(pattern, str)
Example
import re
if match:
print("pattern found inside the string")
else:
print("pattern not found")
Match object: You can get methods and attributes of a match object using dir()
function. Some of the commonly used methods and attributes of match objects
are:
match.group(): The group() method returns the part of the string where t
here is a match.
Example
import re
string = '39801 356, 2102 1111'
# Three digit number followed by space followed by two digit number
pattern = '(\d{3}) (\d{2})'
if match:
print(match.group())
else:
print("pattern not found")
# Output: 801 35
>>> match.group(2)
'35'
>>> match.group(1, 2)
('801', '35')
>>> match.groups()
('801', '35')