Minor - Asssignment-9 - Files and Exceptions
Minor - Asssignment-9 - Files and Exceptions
1. Write a function that takes two file names, file1 and file2 as input. The function should read the con-
tents of the file file1 line by line and should write them to another file file2 after adding a newline at
the end of each line.
2. Write a function that reads a file file1 and displays the number of words and the number of vowels in
the file.
3. Write a function that takes data to be stored in the file file1 as interactive input from the user until
he responds with nothing as input. Each line (or paragraph) taken as input from the user should be
capitalized, and stored in the file file1.
4. Write a function that reads the file file1 and copies only alternative lines to another file file2. Alterna-
tive lines copied should be the odd numbered lines. Handle all exceptions that can be raised.
5. Write a function that takes two files of equal size as input from the user. The first file contains
weights of items and the second file contains corresponding prices. Create another file that should
contain price per unit weight for each item.
6. Write a function that reads the contents of the file Poem.txt and counts the number of alphabets, blank
spaces, lowercase letters and uppercase letters, the number of words starting with a vowel, and the
number of occurrences of word ’beautiful’ in the file.
7. What will be the output produced on executing function inverse1 when the following input is entered
as the value of variable num:
(a)5 (b)0 (c)2.0 (d)x (e)None
def inverse1():
try:
num = input(’Enter the number: ’)
num = float(num)
inverse = 1.0 / num
except ValueError:
print(’ValueError’)
except TypeError:
print(’TypeError’)
except ZeroDivisionError:
print(’ZeroDivisionError’)
except:
print(’Any other Error’)
else:
print(inverse)
1
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
finally:
print(’Function inverse completed’)
9. Identify two exceptions that may be raised while executing the following statement:
result = a + b
10. What will be the output for the following code snippets if the file being opened does not exist:
• (a)
textbackslash nnewline try:
f = open(’file1.txt’, ’r’)
except IOError:
print(’Problem with Input Output...\n’)
else:
print(’No Problem with Input Output...’)
• (b)
try:
f = open(’file1.txt’, ’w’)
except IOError:
print(’Problem with Input Output...\n’)
else:
print(’No Problem with Input Output...\n’)
11. Consider the following program. Check for the error (if any), otherwise write the output.
2
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
f=open(’PYTHON’,’w’)
f.write(’ ”i am great ” and ’)
f.write(’ ” failure is a part of success” ’)
f=open(’PYTHON’,’r’)
print(f.read())
f.close()
12. Consider the following program. Check for the error (if any), otherwise write the output.
f=open(’file1’,’r’)
f.write(’”work is worship”’)
f.close()
(a). f=open(’PYTHON’,’w’)
f.write(’failure is a part of success’)
f = open(’PYTHON’, ’r’)
print(f.read(4))
f.close()
(b). f=open(’PYTHON’,’w’)
f.write(’failure is a part of success’)
f = open(’PYTHON’, ’r’)
print(f.read())
f.close()