write a python program create employee txt file and store employee name and address

Hi Guys, welcome to my blog in this article you will learn about how to Write A Python Program To Create An Employee Txt File And Store Employee Name And Address

 

Title : 

Write A Python Program To Create An Employee Txt File And Store Employee Name And Address



Write A Python Program To Create An Employee Txt File And Store Employee Name And Address


 

Description : 

Hey Guys, In this article let me explain to you how to write a python program to create a text file of an employee and then write employee name and their address into the file.

 

 

There are two ways to solve this, let's look into first Lets first open the file in write mode, use infinite while loop to ask the user to enter employee name and address and at same time write it into a file. If the user enters quit, break The Infinite while loop and exit the program by printing the employee data that has been written into the text file.


    
fd = open("employee.txt", "w")

while True:
   name = input("Enter Employee Name (Type Quit to exit) : ")
   if name.lower() == "quit":
       break
   address = input("Enter Employee Address : ")
   fd.write(f"Name : {name}, Address : {address}")

print("Data has been written into the file")
fd.close()


    


Now let's looking to the second version of the same program Let's begin with asking a user to enter the count of number of employees, using the for loop to iterate all the employees and keep asking employee name and their address. at the same time write the employee name and address into the file. Once the for loop completes close the file


    
employee_count = int(input("Enter Number of Employees :"))

fd = open("employee.txt", "w")
for _ in range(employee_count):
   name = input("Enter Employee Name :")
   address = input("Enter Employee Address : ")
   fd.write(f"Name : {name}, Address : {address}")

print("Data has been written into the file")
fd.close()


    




Complete Python Code : 


    
Code 1
===============================
fd = open("employee.txt", "w")

while True:
   name = input("Enter Employee Name (Type Quit to exit) : ")
   if name.lower() == "quit":
       break
   address = input("Enter Employee Address : ")
   fd.write(f"Name : {name}, Address : {address}")

print("Data has been written into the file")
fd.close()





Code 2 
===============================

employee_count = int(input("Enter Number of Employees :"))

fd = open("employee.txt", "w")
for _ in range(employee_count):
   name = input("Enter Employee Name :")
   address = input("Enter Employee Address : ")
   fd.write(f"Name : {name}, Address : {address}")

print("Data has been written into the file")
fd.close()





    

 

 

Output : 


    
Code 1 Output
======================
Enter Employee Name (Type Quit to exit) : Rajesh
Enter Employee Address : Delhi
Enter Employee Name (Type Quit to exit) : Shyam
Enter Employee Address : Mumbai
Enter Employee Name (Type Quit to exit) : quit
Data has been written into the file



    

 

Output : 


    
Code 2 Output
======================
Enter Number of Employees :2
Enter Employee Name :James
Enter Employee Address : New Jersey
Enter Employee Name :Stalin
Enter Employee Address : Berlin
Data has been written into the file


    



Conclusion :

In this blog, we discussed how to write a python program. Create an employee text file and write the contents inside it. fd.write(f"Name : {name}, Address : {address}") The above right function is used to write the contents into the text file, you can also modify this right statement according to your usage. We discussed to methods to solve the same program and write the contents of employee into the text file Comment it down below if you have any query is regarding the above Python program

 

Post a Comment

0 Comments