Introduction to Python Copy File
Python copy file function is a high-level operation in python. It is used to copy files from one location to another location. Python interacts with the operating system to perform this operation, and before carrying this operation, we need to import os module and shutil modules; without these modules, this operation cannot be done. We can copy single files and multiple files present in the directory. Though these functions are high-level operations, still they don’t copy metadata, owner information, ACLs, and alternate data streams are not copied. Source and destination paths are like objects or string.
Destination file path names should be proper and according to the rules of the copy function. For example, the source and destination file path should not be the same; otherwise, python will generate the same file error.
Syntax
shutil.copyfile(src, dst, *, follow_symlinks=True)
- src: complete source file, and it should be in string format. This parameter is mandatory.
- dst: destination file path, and it should be also in string format and not the same as the source. This parameter is mandatory.
- follow_symlinks: This is the optional parameter, and the default value is true. If the value is false and it is symbolic links, then the file will not be copied; instead, a new symbolic link is created.
The destination file path should be writable; if the directory does not have write permission, then python will return OSError. This function does not allow copying special files like pipes, characters, or block devices.
Examples of Python Copy File
Given below are the examples of the Python Copy File:
Example#1
Code:
import os
import shutil
shutil.copy('Python/original.txt', 'Python/duplicate.txt')
Output:
This is a very basic example of shutil.copy function. We have imported the os and shutil library. In the shutil.copy function, we have specified the first parameter as a source file, and the second parameter is the destination file. In this case, the source and destination folder are the same, but file names are different so that they will be successfully copied.
Example #2
Code:
import os
import shutil
dir = '/Users\Big Data Cluster\Documents\Python\Testing'
Source = '/Users\Big Data Cluster\Documents\Python\Testing\Sample.txt'
Destination = '/Users\Big Data Cluster\Documents\Python\Testing\Test.txt'
print(os.listdir(dir))
shutil.copy(Source,Destination)
print(os.listdir(dir))
Output:
In the above program, we have imported the required library. We have specified the directory path in the dir variable. We have stored the source file path in the source variable and the destination file path in the destination variable. Finally, we have passed the source and destination variables in the shutil.copy function.
We have the os.listdir method to list all the files that are present in the directory. As we can see that before the shutil.copy method, there was one file, ‘Sample.txt’ inside the directory, and after the function was executed file was copied and named as ‘Test.txt.’
Example #3
Code:
import os
import shutil
Source = '/Users\Big Data Cluster\Documents\Python\Testing\Sample.txt'
Destination = '/Users\Big Data Cluster\Documents\Python\Testing\Test.txt'
shutil.copy(Source,Destination)
Output:
In the above example, we have created two variables, source and destination holding paths for source and destination files. But in this example, as you can see, we have specified the same file path in both source and destination. In this shutil.copy function will return the same file error, as you can see in the above screenshot.
Conclusion
Python provides a shutil.copy method to copy our file from one location to another location. This is a very easy and useful method for handling files; the only drawback is that it doesn’t copy the file’s metadata information.
Recommended Articles
This is a guide to Python Copy File. Here we discuss the Examples of the Python Copy File along with the syntax, codes, and outputs. You may also have a look at the following articles to learn more –