How to copy files in python?

0
0

In Python, you can copy files using the shutil module. The shutil module provides a copy() function that can be used to copy a file from one location to another. Here’s an example:

import shutil

# Copy a file from one location to another
shutil.copy(‘/path/to/source/file’, ‘/path/to/destination/folder’)

In this example, shutil.copy() is used to copy a file from the source location to the destination folder. Note that the destination should be a folder, not a file. If you want to copy a file to a new file with a different name, you can use the copy2() function instead and specify the new file name as the destination:

import shutil

# Copy a file to a new file with a different name
shutil.copy2(‘/path/to/source/file’, ‘/path/to/destination/newfile’)

This will copy the source file to a new file with the specified name in the destination folder. The copy2() function preserves more metadata about the file, such as the modification time, so it’s a better choice if you need to preserve those attributes.

  • You must to post comments
Showing 0 results
Your Answer

Please first to submit.