-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
84ab355
commit bfccaca
Showing
2 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
105 changes: 105 additions & 0 deletions
105
src/assets/PythonCopyFileWithShutil/python-copy-paste.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
--- | ||
title: Ways to copy a file with the shutil module in Python | ||
authorName: Salipa Gurung | ||
authorAvatar: https://avatars.githubusercontent.com/u/53458341?v=4 | ||
authorLink: https://github.com/Salipa-Gurung | ||
createdAt: March 11, 2024 | ||
tags: python, copy, shutil | ||
banner: https://blog.jankaritech.com/src/assets/PythonCopyFileWithShutil/images/python_copy_file.png | ||
--- | ||
|
||
Python provides many built-in modules to copy a file. We'll look into ways to copy file using the `shutil` module in this blog post. | ||
|
||
The standard utility module known as shutil is an abbreviation for shell utilities. Numerous functions in the Python shutil module help with high-level file and directory operations. Using the shutil module, there are four common methods that provide an easy and efficient way to copy a file: | ||
|
||
### 1. shutil.copy() method | ||
|
||
```py | ||
shutil.copy(source, destination, *, follow_symlinks=True) | ||
``` | ||
|
||
This method copies the source file to the directory or file specified in the destination and returns the file's destination. | ||
|
||
- shutil.copy() method copies file's data and file's permission mode. | ||
- Metadata (file creation and modification time) of the file copied is not preserved. | ||
- Source and destination should be path-like objects or strings. | ||
- Destination can specify both file or directory. If the destination specifies a path to a directory, the file will be copied to the destination directory with the basename of the source path. | ||
- Copying symbolic link: | ||
|
||
- If `follow_symlinks=True`, the destination will be a copy of the file that the symbolic link in source points to. The default value of parameter `follow_symlinks` is `True`. | ||
|
||
- If `follow_symlinks=False`, destination will be created as a symbolic link. | ||
|
||
```py | ||
# Import shell utility module | ||
import shutil | ||
|
||
# Copy the content of source_file.txt to destination_file.txt | ||
shutil.copy('source_file.txt', 'destination_file.txt') | ||
``` | ||
|
||
### 2. shutil.copy2() method | ||
|
||
```py | ||
shutil.copy2(source, destination, *, follow_symlinks=True) | ||
``` | ||
|
||
The shutil.copy2() method provides additional functionality of preserving all the metadata of the file it can. Other characteristics are the same as for the shutil.copy() method. | ||
- shutil.copy2() method copies file's data and file's permission mode. | ||
- Source and destination should be path-like objects or strings. | ||
- Destination can specify both file or directory. If the destination is a directory, the file will be copied with the same name as the source file name. | ||
|
||
```py | ||
# Import shell utility module | ||
import shutil | ||
|
||
# Copy the content of source_file.txt to destination_file.txt | ||
shutil.copy2('source_file.txt', 'destination_file.txt') | ||
``` | ||
|
||
### 3. shutil.copyfile() method | ||
|
||
```py | ||
shutil.copyfile(source, destination, *, follow_symlinks=True) | ||
``` | ||
Using this method, a file's content is copied from its source to its destination without any metadata. | ||
|
||
- Source and destination should be path-like objects or strings. | ||
- Destination path can only specify a path to a file but cannot specify a path to a directory. | ||
|
||
```py | ||
# Import shell utility module | ||
import shutil | ||
|
||
# Copy the content of source_file.txt to destination_file.txt | ||
shutil.copyfile('source_file.txt', 'destination_file.txt') | ||
``` | ||
|
||
### 4. shutil.copyfileobj() method | ||
|
||
```py | ||
shutil.copyfileobj(fsrc, fdst, length) | ||
``` | ||
|
||
- Source and destination must be file-like objects. | ||
- This method copies contents of the source file object to the destination file object. | ||
- Length parameter, which is optional, specifies an integer value for buffer size. | ||
- It does not preserve metadata | ||
- It does not return any value. | ||
|
||
```py | ||
# Import shell utility module | ||
import shutil | ||
|
||
# Create file objects | ||
source_file_object = open("../hello.txt", "r") | ||
destination_file_object = open("CopyFile/copyFalseSrcPath.txt", "w") | ||
|
||
# Copy content of file object to another file object | ||
shutil.copyfileobj(source_file_object, destination_file_object) | ||
``` | ||
|
||
### Points to prevent common mistakes: | ||
|
||
- The source and destination cannot specify the same path in `shutil.copy()`, `shutil.copy2()` and `shutil.copyfile()`. If the source and destination specify the same file path then exception "SameFileError" will be raised. This error is not raised in shutil.copyfileobj() as it overwrites the file file if it already exists. | ||
- An error "FileNotFound" will be raised if the source specifies a path to a file that is not present. |