Problem 1 tryexcept 7 pts The following function openFile at

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.

Solution

#!/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()

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 openFi

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site