| by Arround The Web | No comments

Python Shutil Move()

In Python, various modules and methods are used to perform/achieve simple to complex tasks. Moving files or directories is considered an important task in Python. The files are moved from one place to another due to several reasons, such as organizing the files, creating backups, or rearranging the structure, and others. To move/transfer a file or directory, the “shutil.move()” method of the “shutil” module is used in Python.

This blog will deliver you a thorough tutorial on the “shutil.move()” method of the “shutil” module using numerous examples. This article will follow the below-provided content:

What is the “shutil.move()” Method in Python?

In Python, the “shutil.move()” method of the “shutil” module is used to move the file from one location “source” to another location “destination”.

Syntax

shutil.move(source, destination, copy_function = copy2)

 

Parameters

In the above syntax:

  • The “source” parameter specifies the string representing the original or source complete path.
  • The “destination” parameter indicates the string representing the file’s final destination path.
  • The “copy_function” parameter indicates the function that is used while copying. The default function is “copy2”.

Return Value

The “shutil.move()” method is used to retrieve the string indicating the path of the newly created file or moved file.

Example 1: Moving File from Original Path to Different Destination Path

This example moves the specified file from the original/source path to the different destination path of the system. The following snippet shows the original path of the file:

In the code below, first, import the “shutil” module. Then, source and destination paths are initialized. After initialization, these paths are passed to the “shutil.move()” method to move the file from source to destination:

import shutil
src = r'C:\Users\p\Documents\program\example.txt'
dest = r'C:\Users\p\Downloads\new_changes\python'
print(shutil.move(src, dest))

 

The above code moves the particular file to the following destination path:

Example 2: Moving Directory from Original Path to Different Destination Path

This example moves the specified directory from the source to the destination using the “shutil.move()” method. The following is the original path of the directory:

In the following code, the “shutil.move()” method takes the original path and destination path of the directory as an argument and moves the directory:

import shutil
src = r'C:\Users\p\Documents\program\python'
dest = r'C:\Users\p\Downloads\new_changes\example'
print(shutil.move(src, dest))

 

As you can see, the above-provided code successfully moved the directory from source to destination:

Example 3: Moving Multiple Files or Directory from the Original Path to a Different Destination Path

We can also move multiple files or directories from the original path to the particular destination path. For example, consider the absolute path of the following multiple files:

Here, first, added the “os” and “shutil” modules along with the initialization of the source and destination path. After that, use the “for” loop to loop over the directory list that contains all the files that exist in the source path. Inside the for loop, invoke the “shutil.move()” method to move all the files one by one to the destination path:

import shutil
import os
dir_1 =r'C:\Users\p\Documents\program'
dest =r'C:\Users\p\Downloads\new_changes'
for file in os.listdir(dir_1):
    file_1 = os.path.join(dir_1, file)
    shutil.move(file_1, dest)

 

The above code executed and moved all files to the specified path:

Example 4: Moving and Renaming the File from the Original Path to the Destination Path

This example renames the file while moving it from the source path to the destination path. Take the following path and file as an example:

In this example, the “source” and “destination” paths are initialized with different file names. Then, call the “shutil.move()” method to move the file from the source to the destination path while changing or renaming the file names:

import shutil
src = r'C:\Users\p\Documents\program\new.txt'
dest = r'C:\Users\p\Downloads\new_changes\example1.txt'
print(shutil.move(src, dest))

 

The execution of the above code displays the same file at the destination path with a different name:

Conclusion

The “shutil.move()” method of the “shutil” module takes two different location paths (source and destination) as a parameter value and transfers the file. This method can be utilized to move single or multiple files along with directories from one location to another. We can also rename the file while moving/transferring it from one place to another. This write-up provided a comprehensive guide on the “shutil.move()” method using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply