Advertisement
DrAungWinHtut

fileio.py

May 14th, 2023
819
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. def ReadFile():
  2.     try:
  3.         file1 = open('a.txt','r')
  4.         data = file1.read()
  5.         print(data)
  6.         file1.close()
  7.     except IOError:
  8.         print('Error reading file')
  9.  
  10.  
  11. def WriteFile():
  12.     try:
  13.         file2 = open('c.txt','x')
  14.         file2.write('\nthis is writing by program')
  15.         file2.close()
  16.     except IOError:
  17.         print('File Error')
  18.  
  19. def SearchFile(key):
  20.     try:
  21.         file = open('a.txt','r')
  22.         lines = file.readlines()
  23.         for data in lines:
  24.             #print(data)
  25.             if data.strip() == key:
  26.                 print(f'{key} is here!')
  27.  
  28.         file.close()
  29.     except IOError:
  30.         print('File Error')
  31.  
  32. # Start Here
  33. # ReadFile()
  34. # WriteFile()
  35. SearchFile('pineapple')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement