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()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.