def safeOpen(filename):
try:
file = open(filename)
except:
file = None
return file
def safeFloat(x):
try:
float(x)
return True
except ValueError:
return False
def averageSpeed():
filename = input("Enter file name: ")
file = safeOpen(filename)
if file == None:
print("File not found. Please try
again")
filename = input("Enter file
name: ")
file = safeOpen(filename)
if file == None:
print("File not
found. Yet another human error. Goodbye")
return None
content = file.readlines()
avg_speed = []
for i in content:
a = i.split()
for j in a:
if safeFloat(j)
and float(j) > 2.0:
avg_speed.append(float(j))
print("Average speed is " + str(round(sum(avg_speed)/len(avg_speed),2)) + " miles per hour")
averageSpeed()
Problem 1 Recall that when the built-in function open() is called to open a file for...
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...
Having some trouble on this Python problem. It has 2 parts. Part 1: Random Number file Writer Write a program that writes a series of random numbers to a file. Each random number should be in the range of 100 through 500. The application should let the user specify how many random numbers the file will hold. sample Outputs Enter the name of the file to which results should be written: ran_numbers_dude.txt Enter the number of random numbers to be...
A. File I/O using C library functions File I/O in C is achieved using a file pointer to access or modify files. Processing files in C is a four-step process: o Declare a file pointer. o Open the desired file using the pointer. o Read from or write to the file and finally, o Close the file. FILE is a structure defined in <stdio.h>. Files can be opened using the fopen() function. This function takes two arguments, the filename and...
Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that will process monthly sales data for a small company. The data will be used to calculate total sales for each month in a year. The monthly sales totals will be needed for later processing, so it will be stored in an array. Basic Logic for main() Note: all of the functions mentioned in this logic are described below. Declare an array of 12 float/doubles...
I need help with this assignment in C++,
please!
*** The instructions and programming style details
are crucial for this assignment!
Goal: Your assignment is to write a C+ program to read in a list of phone call records from a file, and output them in a more user-friendly format to the standard output (cout). In so doing, you will practice using the ifstream class, I'O manipulators, and the string class. File format: Here is an example of a file...