Advertisement
Arcot

File handling

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