Introduction to Python File Handling

Writing Data to a File

 
      name=""
phone=""
choice=0
while choice!=3:
    print("Enter 1 for registration,2 for Search,3 for exit")
    choice=int(input())
    if choice==1:
        name=input("Enter your name")
        phone=input("Enter your phone num")
        file=open("phone.txt","w")
        file.write(name)
        file.write(",")
        file.write(phone)
        file.close()
        print("Phone details registered successfully")
    elif choice == 2:
        print("your name is ",name)
        print("your phone number is ",phone)
    elif choice == 3:
        print("Thanks for using the system")
    else:
        print("Enter a valid input")
    
Output

Reading data from a File

 
      name=""
phone=""
choice=0
while choice!=3:
    print("Enter 1 for registration,2 for Search,3 for exit")
    choice=int(input())
    if choice==1:
        name=input("Enter your name")
        phone=input("Enter your phone num")
        file=open("phone.txt","w")
        file.write(name)
        file.write(",")
        file.write(phone)
        file.close()
        print("Phone details registered successfully")
    elif choice == 2:
        file=open("phone.txt","r")
        print(file.read())
        file.close()
    elif choice == 3:
        print("Thanks for using the system")
    else:
        print("Enter a valid input")
    
Output

Reading from a file using a separator

 
      name=""
phone=""
choice=0
while choice!=3:
    print("Enter 1 for registration,2 for Search,3 for exit")
    choice=int(input())
    if choice==1:
        name=input("Enter your name")
        phone=input("Enter your phone num")
        file=open("phone.txt","a")
        file.write(name)
        file.write(",")
        file.write(phone)
        file.close()
        print("Phone details registered successfully")
    elif choice == 2:
        file=open("phone.txt","r")
        line=file.read()
        name,phone=line.split(",")
        print("your name is",name)
        print("your phone is",phone)
        file.close()
    elif choice == 3:
        print("Thanks for using the system")
    else:
        print("Enter a valid input")
    
Output

Appending data to a file

 
      name=""
phone=""
choice=0
while choice!=3:
    print("Enter 1 for registration,2 for Search,3 for exit")
    choice=int(input())
    if choice==1:
        name=input("Enter your name")
        phone=input("Enter your phone num")
        line_text=name+","+phone
        file=open("phone.txt","a")
        file.writelines(line_text+"\n")
        file.close()
        print("Phone details registered successfully")
    elif choice == 2:
        file=open("phone.txt","r")
        line=file.read()
        name,phone=line.split(",")
        print("your name is",name)
        print("your phone is",phone)
        file.close()
    elif choice == 3:
        print("Thanks for using the system")
    else:
        print("Enter a valid input")
    
Output

Reading multiple Lines from a File

 
      name=""
phone=""
choice=0
while choice!=3:
    print("Enter 1 for registration,2 for Search,3 for exit")
    choice=int(input())
    if choice==1:
        name=input("Enter your name")
        phone=input("Enter your phone num")
        line_text=name+","+phone
        file=open("phone.txt","a")
        file.writelines(line_text+"\n")
        file.close()
        print("Phone details registered successfully")
    elif choice == 2:
        file=open("phone.txt","r")
        for line in file:
            name,phone=line.split(",")
            print("your name is",name)
            print("your phone is",phone)
        file.close()
    elif choice == 3:
        print("Thanks for using the system")
    else:
        print("Enter a valid input")
    
Output

Searching for a phone number in a File

 
      name=""
phone=""
choice=0
while choice!=3:
    print("Enter 1 for registration,2 for Search,3 for exit")
    choice=int(input())
    if choice==1:
        name=input("Enter your name")
        phone=input("Enter your phone num")
        line_text=name+","+phone
        file=open("phone.txt","a")
        file.writelines(line_text+"\n")
        file.close()
        print("Phone details registered successfully")
    elif choice == 2:
        found=False
        search_name=input("Enter the name of the person")
        file=open("phone.txt","r")
        for line in file:
            name,phone=line.split(",")
            if name==search_name:
                print("your name is",name)
                print("your phone is",phone)
                found=True
                break
        file.close()
        if found==False:
            print("No such name in the directory")
    elif choice == 3:
        print("Thanks for using the system")
    else:
        print("Enter a valid input")
    
Output