π Day 89/150 – Copy File Content in Python
Copying the contents of one file to another is one of the most common file handling operations in Python. Whether you're creating backups, duplicating files, or transferring data, Python makes the process simple with built-in functions and modules.
In this post, we'll explore four different methods to copy file content in Python.
Method 1 – Using read() and write()
This is the simplest method for copying the contents of a text file.
File copied successfully!with open("source.txt", "r") as source: content = source.read() with open("destination.txt", "w") as destination: destination.write(content)
print("File copied successfully!")
Output
Explanation
- Open the source file in read mode ("r").
- Read all its contents using read().
- Open the destination file in write mode ("w").
- Write the contents into the destination file.
- If the destination file doesn't exist, Python creates it automatically.
Method 2 – Copy Line by Line
This approach is more memory-efficient because it processes one line at a time.
with open("source.txt", "r") as source, \ open("destination.txt", "w") as destination: for line in source: destination.write(line) print("File copied successfully!")
Output
File copied successfully!
Explanation
- Open both files at the same time.
- Read one line from the source file.
- Immediately write that line to the destination file.
- This method is recommended for large files.
Method 3 – Taking File Names from User
Allow the user to choose the source and destination files.
source_file = input("Enter source file name: ") destination_file = input("Enter destination file name: ") with open(source_file, "r") as source: content = source.read() with open(destination_file, "w") as destination: destination.write(content) print("File copied successfully!")
Sample Input
Enter destination file name: backup.txt
Output
File copied successfully!Explanation
- Accept the source and destination file names from the user.
- Read the source file.
- Write its contents into the destination file.
- Useful when working with different files each time.
Method 4 – Using shutil.copyfile()
Python's shutil module provides a built-in function for copying files.
import shutil shutil.copyfile("source.txt", "destination.txt") print("File copied successfully!")
Output
Explanation
- Import the shutil module.
- Use copyfile(source, destination) to copy the file contents.
- This is the quickest and most convenient method for copying an entire file.
Comparison of Methods
| Method | Best For |
|---|---|
| read() + write() | Small text files |
| Line-by-line copy | Large files |
| User Input | Interactive programs |
| shutil.copyfile() | Fast and simple file copying |
π₯ Key Takeaways
- read() and write() provide a straightforward way to copy file contents.
- Copying line by line is more memory-efficient for large files.
- shutil.copyfile() is the easiest built-in method for copying an entire file.
- Always use the with statement to ensure files are closed automatically.
- File copying is useful for backups, file management, and data migration.


0 Comments:
Post a Comment