Topic: Python File Copy Program using shutil.filecopy
You can easily copy a source file to a destination file. For this purpose, perform the following steps:
- import shutil module, because the shutil module provides the copyfile() function to copy a source file into a destination file.
- input source file name
- input destination file name
- call shutil.copyfile(sourcefile,destinationfile)
You can see the image number 1 given below which shows the situation before execution of Python File Copy Program. Only one text file named example.txt is shown here on d drive.
Check the image number 2 given below to see the output results of the Python file copying program, in which source file example.txt and destination file example1.txt are in the view.
The complete File Copy Python program is as follows:
Python File Copy Program using shutil
import shutil source=input("Enter source file name and path:\n") dest=input("Enter destination file name and path:\n") s=shutil.copyfile(source,dest) print(" The file ' " +source +" ' copied to '" + s + " ' successfully")
Note that this file copy program has no Exception handling code. Please, read more on exception handling in Python File programs:
Python Exception Handling FileNotFoundError
You will also like the following Python File Programs:
-
Find Occurrences of Each Word in Text File
-
Count Words in Each Line of Text File
-
Count Words in Text File
-
Count Lines in Text File
-
Count Vowels in Text File
-
Count Characters in Text File
-
Python Exception Handling FileNotFoundError
-
Python Text File Read and Show Contents Program
-
Python File program read numbers write squares
Pingback: Python Text File Program To Count Vowels | EasyCodeBook.com
Pingback: Python File Phonebook Program | EasyCodeBook.com