Question
Tasks: Task 1: Create and test the openFileReadRobust() function to open file (for reading) whose name is provided from the standard input (keyboard) the name
Tasks:
Task 1: Create and test the openFileReadRobust() function to open file (for reading) whose name is provided from the standard input (keyboard) the name is requested until it is correct
Task 2: Create and test the openFileWriteRobust () function to open file (for writing) whose name is provided from the standard input (keyboard) the name is requested until is correct
Task 3: Extend the program to count number of characters, words and lines in a file by including openFileReadRobust() and openFileWriteRobust () functions
Task 4: Create simple tallying program to input sequence of integers (each integer 0-100) from a file (name to be entered from the keyboard using openFileReadRobust() function, compute the tally sheet discarding all bad data. Display the discarded bad data on the screen.
Task 5: Write the content of the tally sheet (write only non-zero counts of numbers numbers that are in the input file) to a standard output (the shell window).
Task 6: Extend the program to write the content of the tally sheet (write only non-zero counts of numbers numbers that are in the input file) to a file (name to be entered from the keyboard). Display the content of the file on the screen.
Assume the content of the file myData1.txt:
5
100
111
OK
55
5
55
5
The interaction of your program should be displayed in the Shell window similarly as shown below:
Task 1
What is the input file name?
myData.txt
This file cannot be opened
What is the input file name?
myData1.txt
Task 2
What is the output file name?
myResults1.txt
Task 3
The number of characters is: 15
The number of words is: 8
The number of lines is: 8
Task 4
Discarded bad data: 111, OK
Task 5
The tally sheet based on your input file is:
Number of 5s: 3
Number of 55s: 2
Number of 100s: 1
Task 6
The tally sheet written to the file: myResults1.txt
Copy of the content of the file myResults1.txt is:
Number of 5s: 3
Number of 55s: 2
Number of 100s: 1
Here is what I have so far. I managed to get the first 3 tasks. I need help with the last 3 tasks.
#Task 1
def openFileReadRobust():
fileObjectData = None
while fileObjectData is None:
filename = input('What is the input file name? ')
try:
fileObjectData = open(filename)
except IOError:
print ('This file could not be opened', filename)
return fileObjectData
#Task 2
def openFileWriteRobust(defaultName):
writable = None
while not writable:
filename = input('What is the output file name?')
if not filename:
filename = defaultName
try:
writable = open(filename, 'w')
except IOError:
print ('Sorry, unable to write to file', filename)
return writable
def main():
fileObjectData = openFileReadRobust()
contents = fileObjectData.read()
fileObjectData.close()
print (contents)
print()
fileObjectR = openFileWriteRobust('')
fileObjectR.write('')
fileObjectR.close()
fileObjectRCheck = open('myResults1.txt')
contents = fileObjectRCheck.read()
fileObjectRCheck.close()
print (' ',contents)
print()
#Task 3
fileObjectData = open('myData1.txt')
content = fileObjectData.read()
fileObjectData.close()
chars = len(content)
words = len(content.split())
lines = len(content.split(' '))
print('The number of characters is: ',chars)
print('The number of words is: ',words)
print('The number of lines is: ',lines)
main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started