☞ This function can take argument as a string, list of strings, tuple of strings, dictionary of strings(but it will store key only in the file).
☞ Example 1 :
#writing data using list on a text file
def writing_data():
f = open("demo.txt","w")
L= ["My name is xyz\n", "I am a doctor\n", "I am 28 years old\n"]
f.writelines(L)
f.close()
writing_data()
☞ Example 2 :
#writing single line on a text file
def writing_data():
f = open("demo.txt","w")
s= input("Enter a string : ")
f.writelines(s)
f.close()
writing_data()
☞ Example 3 :
#writing data using dictionary on a text file
def writing_data():
f = open("demo.txt","w")
d = {"a" : "welcome" , "b" : "to", "c" : "coding"}
f.writelines(d)
f.close()
writing_data()
☞ Example 1:
#writing multiple lines using string on a text file
def writing_data():
f = open("demo.txt","w")
while True:
s = input("Enter a line : ")
f.writelines(s + '\n')
choice = input("Do you wish to continue [y/n] : ")
if choice == 'N' or choice == 'n':
break
f.close()
writing_data()
☞ Example 2:
#writing multiple lines using list on a text file
def writing_data():
f = open("demo.txt","w")
L=[]
while True:
s = input("Enter a line : ")
L.append(s + '\n')
choice = input("Do you wish to continue [y/n] : ")
if choice == 'N' or choice == 'n':
break
f.writelines(L)
f.close()
writing_data()