0% found this document useful (0 votes)
19 views3 pages

Minor - Asssignment-9 - Files and Exceptions

The document provides instructions for 15 programming assignments involving file handling and exceptions in Python. The assignments involve writing functions to: 1) Copy lines from one file to another with newlines added 2) Count words and vowels in a file 3) Capitalize and store user input in a file 4) Copy alternative lines from one file to another 5) Create a file with price per unit weights from two input files 6) Count elements in a text file 7) Handle exceptions from invalid function inputs 8) Handle exceptions in a percentage calculation function 9) Identify exceptions from arithmetic operations 10) Handle file open exceptions 11) Write to and read from a file 12) Check for errors

Uploaded by

2104teestaroy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

Minor - Asssignment-9 - Files and Exceptions

The document provides instructions for 15 programming assignments involving file handling and exceptions in Python. The assignments involve writing functions to: 1) Copy lines from one file to another with newlines added 2) Count words and vowels in a file 3) Capitalize and store user input in a file 4) Copy alternative lines from one file to another 5) Create a file with price per unit weights from two input files 6) Count elements in a text file 7) Handle exceptions from invalid function inputs 8) Handle exceptions in a percentage calculation function 9) Identify exceptions from arithmetic operations 10) Handle file open exceptions 11) Write to and read from a file 12) Check for errors

Uploaded by

2104teestaroy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Department of Computer Science and Engineering

Institute of Technical Education & Research, SOA, Deemed to be University

Programming in Python (CSE 3142)


M INOR A SSIGNMENT-9: F ILES 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’)

8. Examine the following function percentage:

def percentage(marks, total):


try:
percent = (marks / total) * 100
except ValueError:
print(’ValueError’)
except TypeError:
print(’TypeError’)
except ZeroDivisionError:
print(’ZeroDivisionError’)
except:
print(’Any other Error’)
else:
print(percent)
finally:
print(’Function percentage completed’)

Determine the output for the following function calls:


(a) percentage(150.0, 200.0)
(b) percentage(150.0, 0.0)
(c) percentage(’150.0’, ’200.0’)

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()

13. Consider the following program.Write the output.

(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()

14. Consider the following program.Write the output.


f=open(’PYTHON’,’w’)
f.write(’failure is a part of success also, i am fine’)
f = open(’PYTHON’, ’r’)
print(f.readline())
f.close()

15. Consider the following program.Write the output.


f = open(’PYTHON’, ’w’)
description =[’we either choose the pain of discipline \n’, ’or\n’ ’the pain of regret\n’]
f.writelines(description)
f.close()
f = open(’PYTHON’, ’r’)
print(f.read())
f.close()

You might also like