What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

3 Ways to Move File in Python (Shutil, OS & Pathlib modules)

  • Jan 02, 2024
  • 6 Minute Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Abrar Ahmed
3 Ways to Move File in Python (Shutil, OS & Pathlib modules)

Files are the building blocks of any system. They store all the information that we require and moving files is important when you want to organize your system or create a well-maintained repository. Python provides several libraries, such as shutil and os, that offer convenient functions to perform these tasks efficiently. In this article, we will learn how to move files in python with code.

How to Move files in Python?

Sometimes we have to move a large number of files and doing it manually is a tedious task. That is where you need to think about automating the process. But automating a process requires coding. Python is a powerful and versatile programming language that can be used for a wide range of tasks, including file manipulation.

The shutil.move() function in python is to move a file from one location to another. It is included with the built-in shutil module which is used for working with files. The OS module or the PATH module can also be used to do the same.

While all these functions work similarly let us dwell deeper into how can we implement them.

1. shutil.move()

The shutil module of Python Standard Library offers a number of practical functions for working with files. The shutil.move() is used to transfer a file between two locations. The source file and the destination are the two arguments given to this method. The file you wish to move is known as the source, and the destination is the location to which you want to move it.

To move a file named "example.txt" from the "C:files" directory to the "C:backup" directory, for instance, use the following code:

import shutil

shutil.move("C:\files\example.txt", "C:\backup\example.

 

The source file is first renamed to the target file name using the shutil.move() method, and then it is deleted. This should be used for moving files and directories across different filesystems because it is accomplished in an atomic manner and is ensured to be secure on all platforms.

It's crucial to remember that the shutil.move() function will overwrite the destination file if it already exists. Instead of overwriting the destination file, you can use the shutil.copy2() function to copy the source file to the desired location. The source file can subsequently be deleted using the os.remove() function.

2. os.rename()

To relocate a file from one place to another, use the os.rename() method in the os module. By providing the entire path of the source and destination files as parameters, it is possible to use this function to relocate a file by renaming the file in the file system.

To move a file named "example.txt" from the "C:files" directory to the "C:backup" directory, for instance, use the following code:

import os

os.rename("C:\files\example.txt", "C:\backup\example.txt")

 

It's vital to understand that the os.rename() method will overwrite the destination file if it already exists. You can verify that the file name already exists before renaming it if you wish to prevent overwriting the destination file and maintain the original file.

This method will only function if the source and destination files are located on the same file system, which is another thing to bear in mind. Use the shutil.move() method instead if you need to move a file between separate file systems.

3. pathlib.Path.rename()

Another method to move files is using pathlib.Path.rename() method. The method, which is a component of the pathlib module, renames the file in the file system. It can be used to move a file by passing the complete paths to the source and destination files as arguments.

To move a file named "example.txt" from the "C:files" directory to the "C:backup" directory, for instance, use the following code:

from pathlib import Path

src_file = Path("C:\files\example.txt")

dst_file = Path("C:\backup\example.txt")

src_file.rename(dst_file)

 

The pathlib.Path.rename works very similarly to the os.rename method. It will overwrite a file if it already exists in the directory, therefore make sure to check if the file name exists before moving the file.

Additionally, the pathlib.Path.rename can only move files in the same file system, unlike the shutil.move() method. The pathlib.Path.rename function can be used to move directories similarly to the os.rename() method in conjunction with the os.rmdir() method. 

Moving Multiple Files in Python

Moving multiple files in Python involves iterating over a list of files and moving each file individually using the shutil.move() function. Let's see an example:

import shutil
import os

source_folder = "path/to/source/folder"
destination_folder = "path/to/destination/folder"

files_to_move = ["file1.txt", "file2.txt", "file3.txt"]

for file in files_to_move:
    source_path = os.path.join(source_folder, file)
    destination_path = os.path.join(destination_folder, file)
    shutil.move(source_path, destination_path)

 

In this example, we specify the source folder path and the destination folder path. We also define a list of files (files_to_move) that we want to move. We then iterate over each file in the list, construct the source and destination paths using os.path.join(), and move each file using shutil.move().

Conclusion

Moving files in Python is an essential task for file management. There are various situations when you have to move a file from one location to another in python. We discussed three different methods to move files in Python. We also explored how to move multiple files at once.

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Abrar Ahmed
An ambivert individual with a thirst for knowledge and passion to achieve. Striving to connect Artificial Intelligence in all aspects of life. I am also an avid coder and partake in coding challenges all the time on Leetcode and CodeChef.