Menu Close

How to copy a file in Python?

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.

Posted in Python, Python Answers