How to copy files

Created by SwatiLinked to 50.9m issues across 117 teams


Copying files can be done using the shutil module in Python. To copy a file, you can use the shutil.copyfile() method. This method takes two parameters: src and dst. src is the source file, and dst is the destination file.

import shutil shutil.copyfile(src, dst)

The destination file must be writable, otherwise an IOError exception will be raised. If the destination file already exists, it will be replaced.

It is important to note that special files such as character or block devices and pipes cannot be copied with this method. If you are using os.path operations, use copy rather than copyfile, as copyfile will only accept strings.