Copy Contents Of One File To Another File In Python

Copy Contents Of One File To Another File In Python

Published on October 28 2021

In this tutorial, you will learn how to read data from a text file and append data to another text file.

ReadData.txt

This is new data.
Python is an programming language.
Welcome to Programming Portal channel.
I love python.

 

WriteData.txt

this is old data.This is new data.
Python is an programming language.
Welcome to Programming Portal channel.
I love python.

 

Source code

file_to_read ="ReadData.txt"
write_to_file="WriteData.txt"

# Reading a file
file = open(file_to_read,"r")
data = file.read()
file.close()

# Writing to a file
with open(write_to_file,"a") as file:   # with method auto closes the file object
    file.write(data)
print('completed')

Video