Advertisement
ZEdKasat

File Handling

Jul 18th, 2021
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | None | 0 0
  1.  
  2. # read a file
  3.  
  4. # Bruce,91923291923,bruce@email.com
  5. # Alfred,3233123124,Alfred@mail.com
  6.  
  7.  
  8. with open("file_name.csv", "r") as file_variable:
  9.     f = file_variable.read() # read complete file
  10.     f = file_variable.readline() # read line by line line1
  11.     f = file_variable.readline() # read line by line line2 # returns None when there is nothing left to read
  12.    
  13.     for line in file_variable: # read all lines one by one
  14.         ls = line.split(",")
  15.  
  16.  
  17. with open("file_name.csv", "w") as file:
  18.     file.write(name + "," + phone + "," + email)
  19.  
  20.  
  21. def saveContact(name, phone, email):
  22.     with open("file_name.csv", "a") as file:
  23.         file.write(name + "," + phone + "," + email + "\n")
  24.  
  25.  
  26.  
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement