Categories
Python Answers

How to copy a file in Python?

Spread the love

Sometimes, we want to copy a file in Python.

In this article, we’ll look at how to copy a file in Python.

How to copy a file in Python?

To copy a file in Python, we can use the copyfile function from the shutil module.

For instance, we write:

from shutil import copyfile

src = './foo.txt'
dst = './bar.txt'
copyfile(src, dst)

We call copyfile with the path of the source and destination files respectively.

Now we should see the src file copied to the dst destination if the file specified by the src path exists.

Conclusion

To copy a file in Python, we can use the copyfile function from the shutil module.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *