Question

Write a function that takes, as an argument, the name of a file, fileName . Your...

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

# Now step through the file and count the matches

for element in file:

if (element== target):

counter = counter + 1

# Print the number of values in the file that equal the target value

print(str(counter) + " matches were found for target value" + str(target))

file.close()

please use simple way which i can understand, i just learn this for a week

0 0
Add a comment Improve this question Transcribed image text
Answer #1

number.txt file contains

4
6
7
9
1
2
5
3

code:

def maxValueInFile(fileName):

# Open the file for reading

file = open(fileName)

maximum = -99999999

#fetch each number from file and check if it is maximum

for number in file:

#convert the number to int, as we read from file it come as string

number = int(number)

#if the current number is greater than maximum store the number in maximum

if number > maximum:

maximum = number

file.close()

return maximum

print("Maximum number in file : ",maxValueInFile('number.txt'))

Add a comment
Know the answer?
Add Answer to:
Write a function that takes, as an argument, the name of a file, fileName . Your...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PYTHON: Write a function that takes, as an argument, the name of a file, fileName, and...

    PYTHON: Write a function that takes, as an argument, the name of a file, fileName, and an integer n between -750 and 750 (inclusive). Your program should verify that n is an integer in the correct range. If it is not, it should return the string “Your integer is out of range.” If it is in the correct range, your program should open (and read through) the file specified, and return the number of values in the file that are...

  • Write a function named "loadStateDict(filename) that takes a filename and returns a dictionary of 2-character state...

    Write a function named "loadStateDict(filename) that takes a filename and returns a dictionary of 2-character state codes and state names. The file has four columns and they are separated by commas. The first column is the state full name and the second column is the state code. You don't have to worry about column 3 & 4. You should eliminate any row that is without a state code. Save the two columns into a dictionary with key = state code...

  • 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 r...

    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...

  • in python im trying to get this to print to the file i it created def...

    in python im trying to get this to print to the file i it created def calculateTotal(): filename = input("Enter a name for the file : ")#ask users for a name for file no_of_students = int(input("Enter the number of students in class : ")) data = [] with open(filename,"w+") as f: for index in range(no_of_students): grades = input("Enter Name of student plus grade :").split() # store the student data in array "data" data.append(grades) # store the total grades in "total"...

  • Consider the following function: def get_largest(filename): input_file = open(filename, 'r') lines = input_file.readlines() input_file.close() largest =...

    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...

  • Write a class called TemperatureFile. • The class has one data attribute: __filename • Write get...

    Write a class called TemperatureFile. • The class has one data attribute: __filename • Write getter and setter for the data attribute. • Write a “calculateAverage” function (signature below) to calculate and return average temperature of all temperatures in the file. def calculateAverage(self): • Handle possible exceptions Write a main function: • Create a file ('Temperatures.txt’) and then use “write” method to write temperature values on each line. • Create an object of the TemperaureFile with the filename (‘Temperatures.txt’) just...

  • In Python: LoadFile is a function that takes in a string (a filename) and then returns...

    In Python: LoadFile is a function that takes in a string (a filename) and then returns a list. The list is the contents of the file, where each element is a list of data from the file. Here's an example of using this function. The input file had four lines of text. >>> lines = LoadFile("test.txt") >>> print("OUTPUT", lines) OUTPUT ["Hello there", "I am a test file", "please load me in and print me out", "Thanks"]

  • python 3 HW11.2. Read a file and print its lines The function takes a single string...

    python 3 HW11.2. Read a file and print its lines The function takes a single string parameter, which is the name of a file. Complete the function to open the file for reading, read the lines of the file, and print out each line. The function takes a single string parameter, which function to open the file for reading, read the lines student.py def print_lines of file (filename): 1

  • 2. Write a function file_copy() that takes two string parameters: in file and out_file and copies...

    2. Write a function file_copy() that takes two string parameters: in file and out_file and copies the contents of in_file into out file. Assume that in_file exists before file_copy is called. For example, the following would be correct input and output. 1 >>> file_copy( created equal.txt', 'copy.txt) 2 >>> open fopen ( copy.txt') 3 >>> equal-f = open( 'created-equal . txt') 4 >>equal_f.read () 5 'We hold these truths to be self-evident, Inthat all men are created equalIn' 3. Write:...

  • HELP! HOW WOULD I WRITE THIS IN PSEUDOCODE? from itertools import accumulate from collections import Counter...

    HELP! HOW WOULD I WRITE THIS IN PSEUDOCODE? from itertools import accumulate from collections import Counter def addvalues(a, b): if type(a) is str: a = a.strip("\n") if type(b) is str: b = b.strip("\n") return int(a) + int(b) def maxValues(a, b): if type(a) is str: a = a.strip("\n") if type(b) is str: b = b.strip("\n") a = int(a) b = int(b) return max(a, b) def main(): counter = 0 total = 0 maximum_cars = 0 file = open("MainAndState.dat", "r") maximum =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT