Python program to Write data into File in Python

fp = open("C:/Users/USER/Desktop/Python.txt",'w')
fp.write("Hello World 1 \n")
fp.write("Hello World 2 \n")
fp.writelines("Hello World 3 \n")
text = ["Hello World 4 \n","Hello World 5 \n", "Hello World 6"]
fp.writelines(text)
fp.close()
fp = open("C:/Users/USER/Desktop/Python.txt")
print(fp.read())
fp.close()

 

 

 

Output:

 

 

Hello World 1
Hello World 2
Hello World 3
Hello World 4
Hello World 5
Hello World 6

 

 

Subscribe For Daily Updates

100+ Python Pattern Examplespython pattern examples - star patterns, number patterns