#
Import math
Print (dir(math))
Open (“test.txt”,`w’,encoding =’utf -8) as f:
f.write(“my first file \n”)
f.write(“this file\n”)
f.write(“contains three lines \n”)
F=open(“test.txt”,’r’,encoding = ‘utf-8’)
Print(f.reead())
F=open(“test.txt”,’r’,encoding =’utf-8’)
f.read()
print(f.read(4))
f.seek(0)
print(f.read(4))
F=open(“test.txt”,’r’,encoding =’utf-8’)
For line in f:
Print(line)
F=open(“test.txt”,’r’,encoding =’utf-8’)
For line in f :
Print(line,end=”)
F=open(“test.txt”,’r’,encoding =’utf-8’)
Print(f.readline)
Print(f.readline)
#
Import os
Current –directory = os.get cwd()
Print(current – directory)
Import os
Folder-path= ‘c:\users\\ausus\\dekstop\\test-directory
Result = os.listdir(folder_path)
Print(result)
Import os
Os.mkdir(‘test’)
Import os
Os.rename (‘test’,’new-test)
Import os
Os.rename(‘test’,’c:/users/asus/desktop/test’)
Import os
Os.listdie() (“new-one”,’old.txt’)
Os.remove(‘old.txt’)
Os.listdir()(‘new-one’)
Os.rmdir()[‘new-one’]
Os.rmdir(‘new-one)
Os.listdir()
[]
Import shutil
Shutil.rmtree(‘test’)
#
Traceback (most recent call list):
File “..\test.py”,line 1,in <module>
Print(1/0)
Def find reciprocal (value):
Try:
Print(“value:”,value)
R=1/value
Print(“the reciprocal of”,value, “is”,r,”\n”)
Except:
Print(“you cannot find reciprocal of”,value,”\n”)
Find reciprocal (“hello”) find reciprocal(2)
Def find reciprocal (value):
Try:
Print(“value:”,value)
r=1/value
Print(“reciprocal of”,value,”is”,r)
Except value error:
Print(“you got value error”)
Except zero division error:
Print(“handling all other errors”)
Find reciprocal (0)
A=int(input(“enter a positive integer:”)
Try: if a<=0:
Raise value error (“not a positive number)
Print(“you entered “,a)
Except value error as ve:
Print(ve)
Try: F=open(“test.txt”,’r’,encoding =’utf-8’)
Finally:f.close()
Class error (exception):
Class
Value too small error(error):
Class
Value too large error (error):
Number=10
While true :
Try:
i_num=int(input(“enter a number:”))
if i_num<number:
raise
value too small error
elif i_num>number:
raise
value too large error:
break
except value too small error:
print(“this value is too small,try again”)
except value too large error:
print(“this value is too large,try again”)
print(“you guessed it correctly:”)