To write a python program for copying the contents from one file to another file.
PC Anaconda - Python 3.7
Create a function that takes two inputs, an existing file name and a new file name.
Open the existing file in read mode.
Open the new file in write mode.
Read the contents on existing file using read() and store it in a variable.
Copy the variable data into the new file using write().
Call the function.
''' Python program for copying the contents from one file to another file. Developed by: Dhivya Shri. B RegisterNumber: 21002377 '''
def copy(fname,newfile):
with open(fname,'r')as fp:
with open(newfile,'w')as fp1:
data1=fp.read()
fp1.write(data1)
fname=input("Enter an existing file: ")
newfile=input("Enter a name for new file: ")
copy(fname,newfile)
New Text File "File3.txt":
Output of the code (to receive input):
Thus the program is written to copy the contents from one file to another file.