Header Ads Widget

Responsive Advertisement

write a python program to read contents of first.txt file and write same content in second.txt file

Python Program to Read Contents of first.txt File and Write Same Content in second.txt File

In this article you will learn about a Python program that reads first file and copies the content of first file line by line to the second file until complete contents is copied. 


Python supports File IO operations open() function call so we open the second file in right word and again use open() function call to open the first file in a read mode. Keep reading The first file line by line and right 8 in second file until we reach end of file using for loop.


Once we reach end of file(EOF) close the second file that is in write mode.

Below is the Python Program to Read Contents of first.txt File and Write Same Content in second.txt File


filehandle = open("second.txt", "w")
for x in open("first.txt", "r").readlines():
filehandle.writelines(x)
filehandle.close()

 

To test this program open text file first.txt and fill it with contents, then run this program, what's the program is executed successfully you can see that second.txt file is created with the same contents as first.txt

Post a Comment

0 Comments