<jupyter_start><jupyter_text>**Question-1**Develop a python program to open a file in read only mode and try writing something in it and handle the subsequent errors using Exception Handling.<jupyter_code># ** lets create a file and write something in it so that we can use it**

file = open('mdarman1.txt','w') 
file.write("Exception Handling on files")  
file.close()
file = open('mdarman1.txt', 'r') 
for each in file: 
    print(each)
# ** now use exception handling **

try:
    file = open('mdarman1.txt','r')
    for each in file:
        print(each)
    file.write("Hello ! This is how exceptions are handled")
except Exception as e:
    print("This Exception Occurred - ",e)
finally:
    print("The finally block will always execute whether any of the try or except executes or not")
    file.close()<jupyter_output>Exception Handling on files
This Exception Occurred -  not writable
The finally block will always execute whether any of the try or except executes or not<jupyter_text>** Question-2Write a python function for finding a number is prime or not and perform the unit testing using the pylint and the unit test library<jupyter_code>pip install pylint
%%writefile prime_check.py
"""
This is a Python program for checking Prime Numbers
"""
def prime_check(number):
    '''
    This is Python function to check Prime Numbers
    '''
    if number <= 1:
        return"Not a Prime Number"
    if number <= 3:
        return"Prime Number"
    if number % 2 == 0 or number % 3 == 0:
        return"Not a Prime Number"
    i = 5
    while i * i <= number:
        if number % i == 0 or number % (i + 2) == 0:
            return"Not a Prime Number"
        i = i + 6
    return"Prime Number"


# ** Running pylint on the prime_check.py**

! pylint prime_check.py
# **Unit Testing**

%%writefile test_prime.py

import unittest
import prime_check as pc

class TestPrime(unittest.TestCase):
    def test_non_prime(self):
        number = 35
        result = pc.prime_check(number)
        self.assertEqual(result,"Not a Prime Number")
    def test_prime(self):
        number = 11
        result = pc.prime_check(number)
        self.assertEqual(result,"Prime Number")

if __name__ == "__main__":
    unittest.main()

# ** Running unit testing**

! python test_prime.py<jupyter_output>..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK
{'fraction_non_alphanumeric': 0.12160468031759299, 'fraction_numerical': 0.010447137484329294, 'mean_word_length': 2.8990228013029316, 'pattern_counts': {'":': 1, '<': 10, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '192697', 'license': 'MIT', 'n_tokens_mistral': 726, 'n_tokens_neox': 684, 'n_words': 273, 'path': 'ExceptionHandling-and-UnitTesting.ipynb', 'repo_name': 'mdarman187/basicPython', 'size': '2393'}