import shutil # Copy the file 'source.txt' to 'destination.txt' shutil.copy('source.txt', 'destination.txt')
To copy a file in Python, you can use the shutil module's copy() method. This method takes two arguments: the source file and the destination.
This method will create a new file at the destination, and the contents of the source file will be copied into it. If a file with the same name already exists at the destination, it will be overwritten.
It's important to note that this method only copies the contents of the file. Any metadata associated with the source file (such as permissions, timestamps, etc.) will not be preserved in the destination file. If you need to copy a file and preserve its metadata, you can use the copy2() method instead, which is also part of the shutil module.
Learn Flask development and learn to build cool apps with our premium Python course on Udemy.
Subscribe to be the first to know when a new Python course is released.