inputFileObj = open( filename, mode )
write code to close the file.# A real example, but an INCORRECT solution import sys filename = "letters.txt" mode = 'r' # read mode try: inputFileObj = open( filename, mode) for line in inputFileObj: line = inputFileObj.readline() print(line, end='') except: print("ERROR! Unable to open or read from", filename) sys.exit() finally: inputFileObj.close()
# A real example, but an INCORRECT solution import sys filename = "letters.txt" mode = 'r' # read mode try: inputFileObj = open( filename, mode) line = inputFileObj.readline() while line != '': line = inputFileObj.readline() print(line) except: print("ERROR! Unable to open or read from", filename) sys.exit() finally: inputFileObj.close()
import sys # exit() filename = 'completely/bogus/path/data.txt' mode = 'w' # write mode try: dataFileObj = open( filename, mode ) print('I just won the lottery!!!') except: print('ERROR! Unable to open', filename) sys.exit() print('Yeah,', filename, 'open successfully')
Sarah Adams Deposit 350.00 Withdraw 200.00 Withdraw 50.00 Deposit 150.00 Deposit 100.00 Withdraw 200.00Your task is to read the data from this file, calculate the resulting balance after all the transactions, and write customer's name and their updated balance in a new file called summary.txt:
Sarah Adams: $150.00