Thursday, January 9, 2025
HomeProgrammingPython | shutil.copyfile() method

Python | shutil.copyfile() method

The shutil.copyfile() method in Python is used to copy the contents of one file to another. It takes two arguments: the source file path and the destination file path. This method only copies the file contents, not metadata (like permissions or timestamps). If the destination file already exists, it will be overwritten.

See also  How to delete a docker image?

Here’s a basic example:

import shutil
shutil.copyfile(‘source.txt’, ‘destination.txt’)

In this example, the contents of source.txt are copied to destination.txt. If the destination file does not exist, it will be created. The method returns the path of the destination file.

See also  How to Compare Two Objects in Java

It’s important to note that shutil.copyfile() raises an error if the source file doesn’t exist or if the destination is a directory. For more advanced file copying, such as including file permissions, shutil.copy() or shutil.copy2() can be used

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x