You are on page 1of 6

Workshop 6:

File Input and Output

Walk through exercise - DateTime


Datetime library provides standardised ways to extract
date and time related information, process date and time,
as well as formatting the display. It comes with 4 basic
objects:
- date
- time
- datetime (combine date and time)
- timedelta (duration, time difference)

https://docs.python.org/3/library/datetime.html

1
Walk through exercise – date
datetime.date(year, month, day)

Step 1 from datetime import datetime, date, time, timedelta

Step 2 to_day = date.today()

Step 3 print(to_day)

Todo: Explore the properties of date 
to print the day, year, month …

Task a print(to_day.xxxx)

Walk through exercise – timedelta


datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)

Step 1 import datetime

Step 2 to_day = datetime.date.today()


Try adjusting 
Step 3 one_day = datetime.timedelta(days=1) different duration 
using the 
Step 4 properties from 
print(to_day + one_day)
timedelta

2
Walk through exercise – datetime
datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0)

Step 1 from datetime import datetime, date, time, timedelta

Step 2 independence_day = datetime(year=1965, month=8, day=9)

Step 3 print(independence_day )

Todo: print out how old is Singapore this year?

Task b print(xxxx)

Walk through exercise – strftime


date.strftime(format)

Returns a string representing the date following the given format. Can be called from 
date, datetime objects. 
The following example prints out the current date and time
Step 1 from datetime import datetime, date, time, timedelta
Other useful directives:
Step 2 moment = datetime.now() %a ‐ day of the week (short form)
%A ‐ day of the week 
%B ‐ Month in English
Step 3 print(moment.strftime("%d/%m/%y")) %Y ‐ year
%m ‐ month
Step 4 print( moment.strftime("%H:%M")) %d ‐ day
%H ‐ hour 
%M ‐ minute
%S ‐ second

3
Task 6.1 File Creation (Text File)
Complete the following python program to ask user for name, and body
temperature in celsius. Record down current the details (date, time,
name and temperature) into a text file named: “temperature.txt”.

filename = _________________ # create a variable to keep file name
name = ______("May I have your name? ") # Getting user input
temperature = ______("What's your body temperature in celsius?")
moment = _________.now() # Get current date and time
datenow = _______.________("%d/%m/%y") # Obtain a string display on date today from moment
timenow = ________._______("%H:%M") # Obtain a string display on time now from moment

file = ______(filename, ___) # open the file for writing
# construct a row representation containing the details from the above variables
row = datenow + “," + ____________ + "," + ____________ + "," + ____________
file.______(____) # write the row to file
____.______() # closing the file

Task 6.2 File Creation (CSV File)


Complete the following python program to ask user for name, and body
temperature in celsius. Record down current the details (date, time,
name and temperature) into a text file named: “temperature.txt”.

import ___

filename = _____________
name = _____________
temperature = _____________
moment = _____________
datenow = _____________
timenow = _____________

____ open(filename, ___, , newline="") as _________:
csv_pointer = csv.______(file_pointer)
# create list of data
row = [_________ , _________, _________, _________]
_________.writerow(___)

4
Task 6.3a Revenue Report
1. Write a function that gets user to input the monthly revenues for the
past quarters. It then returns a variable of list data type to caller with
the corresponding values.
2. Then generate a revenue report denoting the month, revenue, and
cumulative total for each month. It ends with a message that sums
up the total revenue. Appropriate formatting has to be in place for
numeric numbers (decimals and comma) and spacing. The output
may look like the following.

Task 6.3b Revenue Report in Text File


Write a python program to import/reuse previous task, make changes to
the function so that it receives input file name from caller to print the
revenue report into a text file. Call the revenue report function to obtain
a quarter’s revenues from user, then print out the report to a text file.

5
Task 6.4 File Reading
Write a Python program to read in the content from the earlier file,
“temperature.txt”. The program will process the data and print out Red
Alert if the temperature is above 37.5 degree Celsius. Try changing the
temperature from text file to verify if it prints out the Red Alert
accordingly. Nothing will be printed if temperature is 37.5 and below.
See the text file on the left diagram and program output from the right.

You might also like