Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 1 try/except (7 pts) The following function openFile() attempts to open

ID: 3927604 • Letter: P

Question

Problem 1 try/except (7 pts)

The following function openFile() attempts to open a file that does not exist, and raises a FileNotFoundError exception.

def openFile():

'open hambone.txt and read content'

infile = open( 'hambone.txt', 'r' )

content = infile.read()

infile.close()

Write a function tryOpenFile()that takes no input arguments. The function should wrap the function call to openFile() in try/except statements to handle the error, so that the program does not enter an erroneous state. When called, the function will print out the following message: "hambone.txt file could not be found"

This means you will need to include the function definition for openFile() in your module.

Test calling tryOpenFile() function in the interactive shell.

Explanation / Answer

#!/usr/bin/python

# Open a file
fo = open("foo.txt", "wb")
print "Name of the file: ", fo.name
print "Closed or not : ", fo.closed
print "Opening mode : ", fo.mode
print "Softspace flag : ", fo.softspace


#!/usr/bin/python

# Open a file
fo = open("foo.txt", "wb")
print "Name of the file: ", fo.name

# Close opend file
fo.close()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote