The file is used to store data permanently. Sometimes we require to move the file location from one path to another path for the programming purpose. This task can be done by using Python script in multiple ways. Move () is the most used method of Python to move the file from one directory to another directory defined in the shutil module. Another way of moving file location by using rename() method that is defined in the os module. These two methods can be used to move the file from one directory to another directory, as explained in this tutorial.

Example-1: Move the file with the original name

The way to move a file from one location to another location with the original name has shown in the following script. The shutil module is imported in the script to use the move() function for moving the file. Path module is imported to use the exists() function for checking the given filename exists or not. If the file exists, the destination path of the file will be defined where the file will be moved. The destination location will be printed after moving the file. If the file does not exist, then an error message will be printed.

# Import shutil module

import shutil

# Import path module from os

from os import path

# Set the filename with path

source_path = “fruits.txt”

# Check the file exist or not

if path.exists(source_path):

    # Set the directory path where the file will be moved

    destination_path = “Files”

    # Move the file to the new location

    new_location = shutil.move(source_path, destination_path)

    # Print the new location of the file

    print(“The %s is moved to the location, %s” %(source_path, new_location))

else:

    # Print the message if the file not exists

    print(“File does not exist.”)

Output

The following output will be appeared after running the above script. Here, the file, fruits.txt, exists, and it has moved to the folder Files.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/image1-29.png" data-lazy- height="607" src="data:image/svg xml,” width=”1349″>

Example-2: Move the file with the new name

The way to move a file from one location to another location by renaming the file has been shown in the following script. shutil and path modules have been imported for moving the file and checking the existence of the file. The new name of the file has defined in the destination path of the file. If the file is moved successfully, then the file path with the new name will be printed other an error message will be printed.

# Import shutil module

import shutil

# Import path module from os

from os import path

# Set the filename with path

source_path = “dept.txt”

# Check the file exist or not

if path.exists(source_path):

    # Set the destination directory path with new name

    destination_path = “Files/department.txt”

    # Move the file to the new location

    new_location = shutil.move(source_path, destination_path)

    # Print the new location of the file

    print(“The {0} is moved to the location, {1}”.format(source_path,new_location))

else:

    # Print the message if the file not exists

    print(“Invalid file path.”)

Output

The following output will be appeared after running the above script. Here, the file, dept.txt, exists, and it has been renamed with the name department.txt and moved to the folder Files.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/image2-28.png" data-lazy- height="602" src="data:image/svg xml,” width=”1345″>

Example-3: Move a folder with multiple files

The way to move a folder with multiple files has been shown in the following script. Here, the source_path variable contains the original folder path, and the destination_path variable contains the destination folder path. The other content of the script is the same as the previous two examples.

# Import shutil module

import shutil

# Import path module from os

from os import path

# Set the directory path of the files to move

source_path = “Images/dice”

# Check the directory path exist or not

if path.exists(source_path):

    # Set the destination directory path

    destination_path = “Files/dice”

    # Move the directory with files to the new location

    new_location = shutil.move(source_path, destination_path)

    # Print the new location

    print(“The {0} is moved to the location, {1}”.format(source_path,new_location))

else:

    # Print the message if the directory path not exists

    print(“Invalid directory location.”)

Output

The following output will be appeared after running the above script. According to the script, the folder dice has moved to the location, Files/dice.

Example-4: Move all files and folders of a particular directory

The way to move the single folder with multiple files has been shown in the previous example. But a folder or directory may contain multiple folders with multiple files also. This example shows the way to move this type of folder to another location. The os module has been imported in this script to use the rename() function that will move the content of the folder with the nested folders and multiple files. listdir() function is used to create a list with the files and folders of the source folder. Next, a for loop has used to iterated the list and moved the content of the source folder to the destination folder by using rename() function.

# Import os module

import os

# Set the directory path of the files to move

source_path = “documents/”

# Check the directory path exist or not

if os.path.exists(source_path):

    # Set the destination directory path

    destination_path = “Files/”

    # Create a list of files and folders of the source path

    filelist = os.listdir(source_path)

    # Iterate the files and folders list

    for file in filelist:

        os.rename(source_path file, destination_path file)

    # Print the new location

    print(“All files and folders of {0} is moved to the location, {1}”.format(source_path, destination_path))

else:

    # Print the message if the directory path not exists

    print(“Invalid directory path.”)

Output

The following output will be appeared after running the above script. According to the script, all the files and folders of the documents folder have moved to the Files folder.

Conclusion

Different ways to move the location of single or multiple files have shown in this tutorial. The uses of shutil and os modules for moving the location of files and folders have been explained in this tutorial using a simple example to easily help the python users do this type of task.

About the author

<img alt="Fahmida Yesmin" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/channel-logo-150×150.jpg605becd1a4329.jpg" height="112" src="data:image/svg xml,” width=”112″>

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.