python – I wrote this code, and I’m struggling to remove readlines, open, and the print statement in my code

I wrote this code, and I’m struggling to remove readlines, open, and the print statement in my code since it says in the homework that we are not allowed to use these statements in the functions file code.

Any help is highly appreciated, I struggling to fix it coz

functions.py file:

def median(file_handle):
    """
    -------------------------------------------------------
    Extracts numbers from a file and calculates their median value.
    Use: median = locate_median(file_handle)
    -------------------------------------------------------
    Parameters:
        file_handle - a file already open for reading (file handle)
    Returns:
        m - median of the values in file_handle (float)
    -------------------------------------------------------
    """
    infile = open(file_handle, "r")
    numbers = ()
    for line in infile.readlines():
        line = line.rstrip()
        words = line.split(",")
        for word in words:
            numbers.append(int(word))

    numbers.sort()
    print(numbers)
    mid = len(numbers) // 2
    if len(numbers) % 2 == 0:
        m = (numbers(mid - 1) + numbers(mid)) / 2
    else:
        m = numbers(mid)
    return m

t02.py file:

from functions import median


file_name = input("Enter file name: ")

median = median(file_name)
print("Median is:", median)

outfile = open("out_t02.txt", "w")
outfile.write("Median is: ")
outfile.write(str(median))

print("Data written into out_t02.txt")
outfile.close()

The numbers.txt file that we have to work with:

10,20,30
40,50,60
90,80,70
11,22,13 
14,14,20

The code’s goal is:

Write a function median that takes a file handle as an parameter and find the median of a
group of scores.

Requirements:

  • Read the group of scores from a file (that contains numbers only, each line has 3
    numbers), and put the scores into a list of numbers.
  • Your function must sort the elements in order. You can use the build-in sort method
  • Find and return the median from the sorted list of scores. Here a median is the middle
  • element of a sorted list if the number of elements is odd, and the average of the two
    middle elements if the number of elements is even.
  • for a list (40,20,50,10,30), the median is 30 as it is the middle element (odd number of
    elements in the sorted list)
  • for a list (40,60,30,10,50,20), the median is 35 as it the average of 30 and 40 (two
    middle items in an even number of elements in the sorted list)

Save the function in a PyDev library module named functions.py .

A sample run for the function:

median(file_handle) returns 35.

Write a testing program named t02.py that tests the median function and write/print the
results to the output file out_t02.txt

• Your function should ask the user for the input file name they would like to open.

• Test your program with the sample file provided numbers.txt.