Ans 1 and 2

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

1) #Type-Based Dispatch

class Time:
def __init__(self,hr=0,mint=0,sec=0):
self.hour=hr
self.minute=mint
self.second=sec
def __str__(self):
return '%.2d:%.2d:%.2d' % (self.hour, self.minute, self.second)
def increment(time,seconds):
time.second+=seconds #245
m=time.second//60 #245//60=4=m
time.second=time.second-(m*60)#245-(4*60)=5=second
time.minute+=m
h=time.minute//60
time.minute=time.minute-(h*60)
time.hour+=h
return time
def add_time(self,other):
sum=Time()
sum.hour=self.hour+other.hour
sum.minute=self.minute+other.minute
sum.second=self.second+other.second
if sum.second>=60:
sum.second-=60
sum.minute+=1
if sum.minute>=60:
sum.minute-=60
sum.hour+=1
return sum
def __add__(self,other):
if isinstance(other,Time):
return self.add_time(other)
else:
return self.increment(other)
print('---------------')
start=Time(9,45)
duration=Time(1,35)
print(start+duration)
print('---------------')
start=Time(9,45)
print(start+1337)

2) #Copying Files and Folders


'''The shutil module provides functions for copying files,
as well as entire folders.Calling shutil.copy(source, destination) will copy the file at the path source
to the folder at the path destination.'''

'''import shutil, os
shutil.copy('F:\\file1.txt','F:\\a1\\5b.txt')''' #copying files

import shutil,os
os.chdir('C:\\')
shutil.copytree('F:\\swings', 'F:\\5c') #copying folders

#file writing
fo = open('F:\\nish.txt', 'a')
fo.write('Hello, disha1!\n')
fo.close()

#Opening file with open() function


#Reading a file using read() function
fo=open('f1.txt')
contents=fo.read()
print(contents)
fo=open('F:\\file1.txt')
contents=fo.read()
print(contents)'''
#Reading a file using readlines() function
fo=open('F:\\file1.txt','r')
contents=fo.readlines()
print(contents)

You might also like