The copy2 method of shutil module
The copy2 method of the shutil module copies a file while preserving its metadata. The first parameter of the method specifies the path to the source file, the second parameter specifies the destination path of the new file. The third optional parameter specifies work with symbolic links (by default, follow_symlinks=True).
Syntax
import shutil
shutil.copy2(where do we copy from, where do we copy to, [mode for symbolic links])
Example
Let's copy the file file1.txt to the file file2.txt:
import shutil
shutil.copy2('file1.txt', 'file2.txt')
The result of the executed code:
file2.txt
See also
-
method
copyof moduleshutil,
which copies a file while preserving the access mode -
method
copytreeof moduleshutil,
which recursively copies a folder -
method
moveof moduleshutil,
which recursively moves a file or directory -
method
path.isfileof moduleos,
which checks the existence of a file -
method
makedirsof moduleos,
which creates a directory -
method
mkdirof moduleos,
which creates one folder -
method
removeof moduleos,
which deletes the file -
method
getcwdmoduleos,
which returns the current working directory