Question

Write the python function time to midnight() that takes the name of a file containing multiple...

Write the python function time to midnight() that takes the name of a file containing multiple lines of text giving time in HH:MM:SS HRS or HH:MM:SS AM or HH:MM:SS PM format one per line. The HRS notation indicates CSE 101 – Summer 2019 Lab Assignment #9 2 that the time has been given in 24-hour time format. Each line tells the current time of the day. The function reads the file line by line and calculates the number of seconds remaining until midnight i.e., 00:00:00 HRS. For each line in the file the function calculates the number of seconds remaining until midnight and appends each into a list. The function then returns that list.

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

def time_to_midnight(): #time to midnight function
   file_name = input("Enter file name: ") #taking file name
   fp1 = open(file_name, 'r') #opening file in read mode
   l=[] #list to store remaining seconds
   for line in fp1: #iterating through each line in file
       time, form = line.split(' ') #splitting every line to time and format based on space
       a =[] #list to store time
       for i in time.split(':'): #splitts time into hours, min, sec
           a.append(int(i))
       form= form.strip() #removing spaces from format
       if (form == "HRS" or form == "AM"): #if the format is HRS or AM then subtract 24-hours and * 3600
           a[0] = (24-a[0])*3600
       elif (form == 'PM'): #if the format is PM then subtract from 12
           a[0] = (12-a[0])*3600
       a[1] = (a[1])*60 #minutes to seconds
       total = (a[0]-a[1])-a[2] #total seconds remaining
       l.append(total) #adding time to list
   print(l) #printing list
  
time_to_midnight()
      


contents in file

ex:

20:15:32 HRS

24 - 20 = 4* 3600 = 14,400 #remaining time to reach midnight

15*60 = 900 #minutes

32 #seconds

sec = 14,400 - 900 = 13,500 - 32 = 13,468.

remaining hours to reach midnight converted to seconds and then the finished minutes and seconds are removed from remaining hours.

Add a comment
Know the answer?
Add Answer to:
Write the python function time to midnight() that takes the name of a file containing multiple...
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
  • Task 1: Write a Python program that takes as input from the user the name of a file containing po...

    python code: Task 1: Write a Python program that takes as input from the user the name of a file containing postcode/location information. Each line in the file consists of the postcode followed by a tab followed by a comma-separated list of locations that have that postcode. For example, the file Small.txt contains: 3015 Newport,South Kingsville,Spotswood 3016 Williamstown 3018 Altona,Seaholme 3019 3021 Albanvale,Kealba,Kings Park,St Albans Braybrook, Robinson Your program should create a list of postcode/location pairs with each pair stored...

  • write a function: Names(path): that reads from a file at location path and returns a dictionary...

    write a function: Names(path): that reads from a file at location path and returns a dictionary mapping from column names to lists containing the data in those columns. The format of the file is a csv file. The first line is a comma separated set of string names for the columns contained in the file. The function will accumulate a dictionary mapping from year to the list of years, from name to the list of names (in the same order),...

  • Write a Python function makeDict() that takes a filename as a parameter. The file contains DNA...

    Write a Python function makeDict() that takes a filename as a parameter. The file contains DNA sequences in Fasta format, for example: >human ATACA >mouse AAAAAACT The function returns a dictionary of key:value pairs, where the key is the taxon name and the valus is the total number of 'A' and 'T' occurrences. For example, for if the file contains the data from the example above the function should return: {'human':4,'mouse':7}

  • 5. Write a Python function called readlinesmton that accepts the name of the file (i.e. filename),...

    5. Write a Python function called readlinesmton that accepts the name of the file (i.e. filename), starting line number i.e. m) and ending line number (i.e. n) as a parameter. The function then returns the contents from line number m to n (m <n). If m < 1 then start from line 1. Similarly, if n > number of lines the file, then stop at the last line in the file. Sample Input: readlinesmton("data.txt",5,7) Sample Input: readlinesmton("data.txt", 0,10)

  • Python Program 5. Write a Python program in a file named validTime.py. Include a function named...

    Python Program 5. Write a Python program in a file named validTime.py. Include a function named string parameter of the form hh:mm: ss in which these are numeric validTime that takes a values and returns True or False indicating whether or not the time is valid. The number of hours, minutes, and seconds must two digits and use a between 0 and 9). The number of hours must be between 0 and 23, the number of minutes and seconds must...

  • (Python 3) Write a program that reads the contents of a text file. The program should...

    (Python 3) Write a program that reads the contents of a text file. The program should then create a dictionary in which the keys are individual words found in the file and the values are the number of times each word appears and a list that contains the line numbers in the file where the word (the key) is found. Then the program will create another text file. The file should contain an alphabetical listing of the words that are...

  • Please use python 3 programming language Write a function that gets a string representing a file...

    Please use python 3 programming language Write a function that gets a string representing a file name and a list. The function writes the content of the list to the file. Each item in the list is written on one line. Name the function WriteList. If all goes well the function returns true, otherwise it returns false. Write another function, RandomRange that takes an integer then it returns a list of length n, where n is an integer passed as...

  • using python The following shows the result of calling function read_from_file_sum_squares() for each of these two...

    using python The following shows the result of calling function read_from_file_sum_squares() for each of these two files: >>> from praco import read_from_file_sum_squares >>> read_from_file_sum_squares () Enter the filename: file1 9 >>> read_from_file_sum_squares () Enter the filename: file2 14 20 1 >>> You can use file = open(filename, "r") to open the file named by string filename to read, and the line list_lines = file.readlines() to store in list_lines the list of strings, where each string is a line of the...

  • Help needed related python task ! Thanx again How you're doing it • Write a function...

    Help needed related python task ! Thanx again How you're doing it • Write a function write_to_file() that accepts a tuple to be added to the end of a file o Open the file for appending (name your file 'student_info.txt') o Write the tuple on one line (include any newline characters necessary) o Close the file • Write a function get_student_info() that o Accepts an argument for a student name o Prompts the user to input as many test scores...

  • Write a Python program named aIP.py which will read data from a file named wireShark.txt and...

    Write a Python program named aIP.py which will read data from a file named wireShark.txt and extract all the pairs of source and destination ip addresses and output them in pairs to another file called IPAddresses.txt , one per line, listing source and destination. Example of output: Source                      Destination 192.168.1.180         239.255.255.250 Detailed Requirements: You will read from a file called wireShark.txt which, to avoid problems with finding paths, will be located in the same directory as your code You will...

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
ADVERTISEMENT