codescracker


python

Python Program to Copy Files



« Previous Program Next Program »


Copy Files in Python

To copy files in python, you have to first import copyfile from shutil library (to use copyfile() function). The copyfile() function copies the content of source file into destination file. After importing copyfile from shutil library, you have to ask to enter name of the source and destination file to perform the copy file operation as shown in the program given here. You may also like these two programs on file:

Python Programming Code to Copy Files

Following python program ask from user to enter name of source and destination file to copy the content of source file into destination file. After performing copy file operation, ask from the user, whether he/she want to display the content of the destination file or not. If he/she want to show the content, then just open the destination file and print all the content present inside it which is the content of the source file:

# Python Program - Copy Files
		
from shutil import copyfile
while True:
	print("Enter 'x' for exit.")
	sourcefile = input("Enter source file name: ")
	destinationfile = input("Enter destination file name: ")
	if sourcefile == 'x':
		break
	else:
		copyfile(sourcefile, destinationfile)
		print("File copied successfully!")
		print("Want to display the content ? (y/n): ")
		check = input()
		if check == 'n':
			break
		else:
			c = open(destinationfile, "r")
			print(c.read())
			c.close()
			print()
		print()

Here is the sample run of the above Python program to demonstrates how to copy the content of one file into another:

python copy files

Same Program in Other Languages

You may also like to learn or practice the same program in other popular programming languages:


« Previous Program Next Program »




Tools
Calculator

Quick Links
Signup - Login - Give Online Test