To write a python program for copying the contents from one file to another file.
PC Anaconda - Python 3.7
from shutil import copy file
from sys import exit
Getting input for source file
Getting input for target file
Giving condition for files
Getting statement from the user to print or not want to print
#Developed by : Priyanka.A
#Register Number: 22008829
import sys
from shutil import copyfile
from sys import exit
source = input("Enter source file with full path:")
target = input("Enter target file with full path:")
# adding exception handling
try:
copyfile(source, target)
except IOError as e:
print("Unable to copy file. %s" % e)
exit(1)
except:
print("Unexpected error:", sys.exc_info())
exit(1)
print("\nFile copy done!\n")
while True:
print("Do you like to print the file ? (y/n): ")
check = input()
if check == 'n':
break
elif check == 'y':
file = open(target, "r")
print("\nHere follows the file content:\n")
print(file.read())
file.close()
print()
break
else:
continue
Thus the program is written to copy the contents from one file to another file.