Skip to content

Commit

Permalink
Add python copy-paste file methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Salipa-Gurung committed Mar 7, 2024
1 parent c6ae9b5 commit b6062a9
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
93 changes: 93 additions & 0 deletions src/assets/PythonCopyFileWithShutil/python-copy-paste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
---
title: Ways to copy file with shutil module in Python
authorName: Salipa Gurung
authorAvatar: https://avatars.githubusercontent.com/u/53458341?v=4
authorLink: https://github.com/Salipa-Gurung
createdAt: Feb 27, 2024
tags: python, copy-paste
banner: https://blog.jankaritech.com/src/assets/PythonCopyFileWithShutil/images/python_copy_file.png
---

Python provides many built-in modules to copy and paste a file. In this blog we are going to look into ways to copy file with shutil module.

The shutil is an abbreviation for shell utilities and it is a standard utility module.
Python shutil module provides many functions which helps to perform high-level file and directory operations. with shutil module, we can copy-paste a file with four different methods:

### 1. shutil.copy() method

```py
shutil.copy(source, destination, *, follow_symlinks=True)
```

This method copies the file of source to the directory or file specified in the destination and returns 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 is directory, the file will be copied with the same name as of the source file name.
- Copying symbolic link:

- If `follow_symlinks=True`, destination will be a copy of the file that the symbolic link in source points to. Default value of parameter `follow_symlinks` is `True`.

- If `follow_symlinks=False`, destination will be created as 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')
```

> If the destination specify path to a file or directory that does not exists, it will raise an error `FileNotFoundError`
### 2. shutil.copy2() method

```py
shutil.copy2(source, destination, *, follow_symlinks=True)
```

It is similar to shutil.copy() method with additional functionality of preserving all the metadata of the file it can.

```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)
```

This method copies content of file without any metadata from source to destination. Destination path cannot specify path to a directory.

### 4. shutil.copyfileobj() method

```py
shutil.copyfileobj(fsrc, fdst, length)
```

- Source and destination must be file-like objects.
- This method copies contents of 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)
```
> Note:
> - The source and destination cannot specify the same path. If source and destination specify same file path then `SameFileError` will be raised.
> - An error `FileNotFound` will be raised if the destination specifies a path to a file that is not present.

0 comments on commit b6062a9

Please sign in to comment.