Problem 1 Recall that when the built-in function open() is called to open a file for reading, but it doesn't exist, an exception is raised. However, if the file exists, a reference to the opened file object is returned. Write a function safeOpen() that takes one parameter, filename a string giving the pathname of the file to be opened for reading. When safeOpen() is used to open a file, a reference to the opened file object should be returned if no exception is raised, just like for the open() function. If an exception is raised while trying to open the file, safeOpen() should return the value None. For example, assuming the file ghost.txt doesn't exist, the following is correct output: >>> # open() >>> print(open('ghost.txt')) Traceback (most recent call last): File "", line 1, in > print(open('ghost.txt')) FileNotFoundError: [Errno 2] No such file or directory: 'ghost.txt" >>> >>> # safeOpen() >>> inputFile=safeOpen('ghost.txt') >>>>> print(inputFile) None >>> Problem 2 Recall that when the built-in function float() is called it returns a floating point number constructed from a number or string. However, if the string doesn't represent a valid floating point value, an exception is raised. Write a function safeFloat() that takes one parameter, x-a number or string that needs to be converted to floating point number. When safeFloat() is used to convert a number or string, an equivalent floating point number is returned if no exception is raised, just like for the float() function. If an exception is raised while trying to convert a number or string, safeFloat() should return 0.0 floating point value. For example, the following is correct output: >>> # float() >>> float('abc') Traceback (most recent call last): File "", line 1, in float('abc') ValueError: could not convert string to float: 'abc' >>> # safeFloat() >>>f=safeFloat('abc') >>> print(f)
Problem 1 Recall that when the built-in function open() is called to open a file for reading, but it doesn't exist, an exception is raised. However, if the file exists, a reference to the opened file object is returned. Write a function safeOpen() that takes one parameter, filename a string giving the pathname of the file to be opened for reading. When safeOpen() is used to open a file, a reference to the opened file object should be returned if no exception is raised, just like for the open() function. If an exception is raised while trying to open the file, safeOpen() should return the value None. For example, assuming the file ghost.txt doesn't exist, the following is correct output: >>> # open() >>> print(open('ghost.txt')) Traceback (most recent call last): File "", line 1, in > print(open('ghost.txt')) FileNotFoundError: [Errno 2] No such file or directory: 'ghost.txt" >>> >>> # safeOpen() >>> inputFile=safeOpen('ghost.txt') >>>>> print(inputFile) None >>> Problem 2 Recall that when the built-in function float() is called it returns a floating point number constructed from a number or string. However, if the string doesn't represent a valid floating point value, an exception is raised. Write a function safeFloat() that takes one parameter, x-a number or string that needs to be converted to floating point number. When safeFloat() is used to convert a number or string, an equivalent floating point number is returned if no exception is raised, just like for the float() function. If an exception is raised while trying to convert a number or string, safeFloat() should return 0.0 floating point value. For example, the following is correct output: >>> # float() >>> float('abc') Traceback (most recent call last): File "", line 1, in float('abc') ValueError: could not convert string to float: 'abc' >>> # safeFloat() >>>f=safeFloat('abc') >>> print(f)
C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter8: I/o Streams And Data Files
Section8.4: File Streams As Function Arguments
Problem 3E
Related questions
Question
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 2 steps
Recommended textbooks for you
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT