Question

In: Computer Science

#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the...

#Write a function called "load_file" that accepts one
#parameter: a filename. The function should open the
#file and return the contents.#
#
# - If the contents of the file can be interpreted as
# an integer, return the contents as an integer.
# - Otherwise, if the contents of the file can be
# interpreted as a float, return the contents as a
# float.
# - Otherwise, return the contents of the file as a
# string.
#
#You may assume that the file has only one line.
#
#Hints:
#
# - Don't forget to close the file when you're done!
# - Remember, anything you read from a file is
# initially interpreted as a string.

Solutions

Expert Solution

# Write a function called "load_file" that accepts one
# parameter: a filename. The function should open the
# file and return the contents.#
#
# - If the contents of the file can be interpreted as
# an integer, return the contents as an integer.
# - Otherwise, if the contents of the file can be
# interpreted as a float, return the contents as a
# float.
# - Otherwise, return the contents of the file as a
# string.
#
# You may assume that the file has only one line.
#
# Hints:
#
# - Don't forget to close the file when you're done!
# - Remember, anything you read from a file is
# initially interpreted as a string.
def load_file(filename):
    try:
        f = open(filename)
        line = f.readline().strip()
        f.close()
        try:
            return int(line)
        except ValueError:
            try:
                return float(line)
            except ValueError:
                return line
    except IOError:
        return ""


Related Solutions

Write a function, named isMultipleOfFive that accepts integer argument. When the function is called, it should...
Write a function, named isMultipleOfFive that accepts integer argument. When the function is called, it should display if the argument "is a multiple of 5" or "is not a multiple of 5".
Write a function called HW5_P1 that accepts 1 input argument: an array, a. The function should...
Write a function called HW5_P1 that accepts 1 input argument: an array, a. The function should output an array, b, that is computed as: b=3a+5. Write a MATLAB function called “fit_line” that accepts 2 input arguments: a column vector of x data and a column vector of y data. The nth element in the input arguments should correspond to the nth Cartesian data point i.e. (xn,yn). The function should compute and return 2 outputs: the slope, m, and the y...
Declare and define a function named getCurrentHours_OR_Month that accepts one Boolean parameter. If the Boolean parameter...
Declare and define a function named getCurrentHours_OR_Month that accepts one Boolean parameter. If the Boolean parameter is true, the function returns current hours; if the Boolean parameter is false, the function returns current month. o This function will obtain the current system time and extract the hours or the month value from the system time depending on the Boolean being received. o This function will return the extracted value as an integer value. o Note that the system time displays...
using python 1. #Write a function called multiply_by_index. multiply_by_index #should have one parameter, a list; you...
using python 1. #Write a function called multiply_by_index. multiply_by_index #should have one parameter, a list; you may assume every item #in the list will be an integer. multiply_by_index should #return a list where each number in the original list is #multipled by the index at which it appeared. # #For example: # #multiply_by_index([1, 2, 3, 4, 5]) -> [0, 2, 6, 12, 20] # #In the example above, the numbers 1, 2, 3, 4, and 5 appear #at indices 0,...
Write a python function image compress() that takes one argument called filename, which is the name...
Write a python function image compress() that takes one argument called filename, which is the name of a file that contains a N × N (N-pixel by N-pixel) “grayscale bitmap image”. A “grayscale bitmap image” is an image of the following form where every pixel contains a grayscale color value between 0 − 255 (inclusive). Colour value 0 means that pixel should appear completely black and color value 255means completely white. Any other value in between stands for different shades...
Write a function for checking the speed of drivers. This function should have one parameter: speed....
Write a function for checking the speed of drivers. This function should have one parameter: speed. 1. If speed is less than 70, it should print “Ok”. 2. Otherwise, for every 5km above the speed limit (70), it should give the driver one demerit point and print the total number of demerit points. For example, if the speed is 80, it should print: "Points: 2". 3. If the driver gets more than 12 points, the function should print: “License suspended”...
program c Write a program called filesearch that accepts two command-line arguments: A string A filename...
program c Write a program called filesearch that accepts two command-line arguments: A string A filename If the user did not supply both arguments, the program should display an error message and exit. The program opens the given filename. Each line that contains the given string is displayed. Use the strstr function to search each line for the string. You may assume no line is longer than 255 characters. The matching lines are displayed to standard output (normally the screen).
In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and...
In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and returns a double. When the variable argument phone contains the character a or A print the word Apple and return 1099.99 When phone contains anything else return a 0.0
Language: Python 3 Compose a function process which accepts a string filename. process should return a...
Language: Python 3 Compose a function process which accepts a string filename. process should return a list of records contained in the file. #define your function here # Create a blank list called `entries` and an empty string called `current_style`. # Open the file `filename`, read the data using readlines(), and close it. # Loop through each line of the file and do the following: # Strip the whitespace off of the ends of the line using the `strip` method....
On Python Write a function that accepts a relative file path as parameter and returns number...
On Python Write a function that accepts a relative file path as parameter and returns number of non-empty lines in the file. Test your function with the provided sample file studentdata.txt.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT