Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Good morning, I am preparin for my finals and was given a study problem based of

ID: 3820017 • Letter: G

Question

Good morning,

I am preparin for my finals and was given a study problem based of a homework question in preparation. The question has multiple parts and I do not have any ideas how to attempt the question. Could someome help with the qustion and also explain how the answered it? I would be really appreciated so I could study each step and understand the coding behind the question. I am using Python 3 also.

1.Program should handle file not found exception, if the input file does not exist

2.Program should raise an exception if the file is empty

3.If the line number provided by the user is not numeric, program should handle the conversion exception

4.If the line number provided is greater than the number of lines in the file, the program should raise a ValueError exception. Note: This may cause the program to exit

Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename. The program should use a function, loadData, to accomplish the following: Load the file content into a dictionary, fileContent, which will contain each line and its corresponding line number. The function should also populate a second dictionary, fileInfo, with each word in the file and the number of times each word appears in the file.

Once the loadData function completes, the program will print the contents of fileInfo dictionary; each word and its count appearing on a separate line.

The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program quits. Otherwise, the program prints the line (text) associated with that line number.

Note: The loadData function take one argument, the name of the file.

Here is the link with my code that I have to add to for the question: http://ideone.com/WeXyds

Please let me know if I worded this confusing and thank you!

Explanation / Answer

import os
def fileData(fname):
fileContent = {}
fileInfo = {}
  
''' It will check whether the filename with the given path exists or not'''
try:
if os.path.isfile(fname):
filename = open(fname, 'r')
content = filename.readlines()
filename.close()
''' It will print the exception if the filename doesn not exists'''
except Exception as e:
print(str(e))
  
try:
''' Adding the contents to a list if the file is not empty'''
for i in range(len(content)):
fileContent[i+1] = content[i]
line = content[i]
for word in line.strip().split():
if word not in fileInfo:
fileInfo[word] = 0
fileInfo[word] += 1
else:
fileInfo[word] += 1
print(fileInfo)
print(fileContent)
'''It will return an exception if the file is empty '''
except Exception as e:
print(str(e))
''' Returning the no.of rows of the given file'''
sumLines = max(fileContent)
print("Their are a total of",sumLines, "lines in the file.")

while True:
choose = input('Enter the line you want read: ')
choose = int(choose)
if choose == 0:
print('Program quits!')
break
elif choose > 9:
print('Invalid line number.')
continue
elif choose < 0:
print('Invalid line number.')
else:
print(content[int(choose)-1])
# continue

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote