Consider the following function:
def get_largest(filename):
input_file = open(filename, 'r')
lines = input_file.readlines()
input_file.close()
largest = 0
for line in lines:
items_list = line.split()
for element in items_list:
value = float(element)
if value > largest:
largest = value
return largest
The function takes a filename as a parameter and returns the largest number in the file as a float. Modify the above function and use a try-except-else block to handle exceptions that may occur and handle the FileNotFoundError in your solution. Note: remember to close the file properly if the file can be opened. You can assume that the minimum number in the file is -9999. If the file is empty the function returns -9999.
For example:
| Test | Result |
|---|---|
print(get_largest('numbers7.txt')) |
6.0 |
print(get_largest('temp')) |
ERROR: The file 'temp' does not exist. |
print(get_largest('empty1.txt')) |
-9999 |
print(get_largest('numbers_with_letters.txt')) |
123.0 |
def get_largest(filename):
#input_file = open(filename, 'r')
#lines = input_file.readlines()
try:
with open(filename, 'r') as input_file:
lines = input_file.readlines()
highest= -9999
for i in range(0,len(lines)):
if highest < int(lines[i]):
highest = int(lines[i])
input_file.close()
return highest
except FileNotFoundError:
return "File not found. Check the path variable and filename"
exit()

print(get_largest('numbers7.txt')) #when file has numbers each on a new line

#when the file is empty it returns -9999

#when the file is not where it returns a message
Consider the following function: def get_largest(filename): input_file = open(filename, 'r') lines = input_file.readlines() input_file.close() largest =...
python
Write the function getSpamLines(filename) that read the filename text file and look for lines of the form 'SPAM-Confidence: float number'. When you encounter a line that starts with "SPAM-Confidence:" extract the floating-point number on the line. The function returns the count of the lines where the confidence value is greater than 0.8. For example, if 'spam.txt' contains the following lines along with normal text getSpamLines('spam.txt') returns 2. SPAM-Confidence: 0.8945 Mary had a little lamb. SPAM-Confidence: 0.8275 SPAM-Confidence: 0.7507 The...
Write a function that takes, as an argument, the name of a file, fileName . Your program should open (and read through) the file specified, and return the maximum value in the file. Name this function maxValueInFile(fileName). def openFile(): # Prompt for the file name filename =input("Enter a file name: " ) # Open the file for reading file = open(filename) # Prompt for the target value target =input("Enter a threshold value: ") # Initialize the counter counter = 0...
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...
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...
I'm a bit confused on how to get this program to run right. Here are the directions: Part 1: Write a Python function called reduceWhitespace that is given a string line and returns the line with all extra whitespace characters between the words removed. For example, ‘This line has extra space characters ‘ ‘This line has extra space characters’ Function name: reduceWhitespace Number of parameters: one string line Return value: one string line The main file should handle the...
Use python
Start:
def main():
gradeList = []
fileName = getFile()
gradeList = getData(fileName)
mean = calculateMean(gradeList)
sd = calculateSD(mean, gradeList)
displayHeadings(gradeList, mean, sd)
curveGrades(mean, sd, gradeList)
if __name__ == "__main__":
main()
For this program, you will create a grade curving program by reading grades from a file, calculating the mean and standard deviation. The mean is the average value of the grades and the standard deviation measures the spread or dispersal of the numbers from the...
C++ Write a function parseScores which takes a single input argument, a file name, as a string. Your function should read each line from the given filename, parse and process the data, and print the required information. Your function should return the number of student entries read from the file. Empty lines do not count as entries, and should be ignored. If the input file cannot be opened, return -1 and do not print anything. Your function should be named...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Copy the following Python fuction discussed in class into your file: from random import * def makeRandomList(size, bound): a = [] for i in range(size): a.append(randint(0, bound)) return a a. Rename the function sumList as meanList and modify it so that it finds the average of the list. The average is the sum divided by the size (len) of the list. Make sure that the function doesn't give you an error when it is called on an empty list. The...
Explain what the code is doing line from line explanations please or summarize lines with an explanation def displayCart(): #displays the cart function """displayCart function - displays the items in the cart ---------------------------------------------------------------------""" import os os.system('clear') print("\n\nCart Contents:") print("Your selected items:", cart) def catMenu(): #function that displays the category menu """catMenu function - displays the categories user picks from ---------------------------------------------------------------------""" import os os.system('clear') print(""" 1 - Books 2 - Electronics 3 - Clothing d - display cart contents x -...