Wednesday 22 September 2021

Python: Move file from one directory to other directory

In this post, I am going to explain two approaches to move a file from one directory to other.

 

Using os.rename method

Signature

os.rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)

 

Rename the file or directory src to dst. This operation may fail on some Unix flavors if src and dst are on different filesystems.

 

a.   If dst exists, the operation will fail with an OSError.

b.   On Windows, if dst exists a FileExistsError is always raised.

c.    On Unix, if src is a file and dst is a directory or vice-versa, an IsADirectoryError or a NotADirectoryError will be raised respectively.

d.   If both are directories and dst is empty, dst will be silently replaced. If dst is a non-empty directory, an OSError is raised

e.   If both are files, dst it will be replaced silently if the user has permission.

 

Example
source_file_1 = '/Users/Shared/test/p1/p1_1/a.txt'
dest_file_1 = '/Users/Shared/test/p1/p2_1/a.txt'
os.rename(source_file_1, dest_file_1)

 

Using shutil.move() method

 

Signature

shutil.move(src, dst, copy_function=copy2)

Recursively move a file or directory (src) to another location (dst) and return the destination.

 

a.   If the destination is an existing directory, then src is moved inside that directory.

b.   If the destination already exists but is not a directory, it may be overwritten depending on os.rename() semantics.

c.    If the destination is on the current filesystem, then os.rename() is used. Otherwise, src is copied to dst using copy_function and then removed.

d.   In case of symlinks, a new symlink pointing to the target of src will be created in or as dst and src will be removed.

e.   If copy_function is given, it must be a callable that takes two arguments src and dst, and will be used to copy src to dst if os.rename() cannot be used.

f.     If the source is a directory, copytree() is called, passing it the copy_function().

 

Example

source_file_2 = '/Users/Shared/test/p1/p1_1/a.txt'
dest_file_2 = '/Users/Shared/test/p1/p2_1/a.txt'
shutil.move(source_file_2, dest_file_2)


Let’s experiment with below directory structure.

$tree
.
├── p1
│   ├── p1_1
│   │   └── a.txt
│   └── p1_2
└── p2
    ├── p2_1
    │   └── a.txt
    └── p2_2

6 directories, 2 files


move_file.py

import os
import shutil

source_file_1 = '/Users/Shared/test/p1/p1_1/a.txt'
dest_file_1 = '/Users/Shared/test/p1/p1_2/a.txt'
os.rename(source_file_1, dest_file_1)

source_file_2 = '/Users/Shared/test/p2/p2_1/a.txt'
dest_file_2 = '/Users/Shared/test/p2/p2_2/a.txt'
shutil.move(source_file_2, dest_file_2)


Run above application, and tree structure will change like below.

$tree
.
├── p1
│   ├── p1_1
│   └── p1_2
│       └── a.txt
└── p2
    ├── p2_1
    └── p2_2
        └── a.txt

6 directories, 2 files







 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment