Python program to write list into file

Write a python program to create list of email-ids and write it into text file with Practical Example

Program Logic:

  • Import random module in program using import statement
  • Create list object to store different set of email-ids into it
  • Ask user to enter email_ids and store it into variable ‘value’ using input method
  • Append list with values entered from user within for loop
  • Open text file say ’email.txt’ in write mode using open method
  • Pass filename and read mode as as argument to open method
  • Apply writelines method to write number of lines into text file say ’email.txt’
  • Pass list say ’email’ as an argument to writelines method
  • Close file ’email.txt’ using close function

Below is implementation code /Source code

import random
email =[]
num = int(input("Enter total number of email_id :"))
for i in range(1,num+1):
    value = str(input("Enter email_id :"))
    email.append(value+'\n')
print("Email_id :",email)
fp = open("email.txt","w")
fp.writelines(email)
fp.close()

Output:

Enter total number of email_id :2
Enter email_id :technocrash.online@gmail.com
Enter email_id :sushil.naik@gmail.com
Email_id : ['technocrash.online@gmail.com\n', 'sushil.naik@gmail.com\n']

Below is Snapshot of executable code with output

Below is email.txt file

Related python programs

  1. Python program to modify or update student data from binary file
  2. Python program to get mode and encoding format of file
  3. Python program to search specific data in binary file
<