How to Move a File or Directory in Python

To move a file in Python:

import shutil

original = r"original path of the file\file_name.file_extension"
target = r"target path to store the file\file_name.file_extension"

shutil.move(original, target)

Steps to Move a File in Python

Step 1: Capture the Original Path

To begin, capture the original path where your file is currently stored.

For example, let’s suppose that a CSV file is stored in a folder called Test_1:

C:\Users\Ron\Desktop\Test_1\Products.csv

Where the file name is ‘Products’ and the file extension is .csv

Step 2: Capture the Target Path

Next, capture the target path where the file will be moved.

For our example, let’s move the CSV file to a folder called Test_2:

C:\Users\Ron\Desktop\Test_2\Products.csv

Step 3: Move the File using Python

You may now utilize this template to move the file to the target location:

import shutil

original = r"original path of the file\file_name.file_extension"
target = r"target path to store the file\file_name.file_extension"

shutil.move(original, target)

Make sure to place the ‘r‘ character before each of your paths to avoid the following error:

‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape

For our example, the code to move the CSV file from the original location (i.e., Test_1) to the target location (i.e., Test_2) is as follows:

import shutil

original = r"C:\Users\Ron\Desktop\Test_1\Products.csv"
target = r"C:\Users\Ron\Desktop\Test_2\Products.csv"

shutil.move(original, target)

Once you run the code in Python (adjusted to your paths), the CSV file will be moved to the Test_2 folder.

Rename the File when Moving it

Alternatively, you can rename your file when you move it to your target location.

For instance, let’s suppose that a new JPG file is stored in the Test_1 folder (where the file name is image).

The code below can then be used to move the file (with the original file name of ‘image‘) to the target location with a new file name (‘new_image‘):

import shutil

original = r"C:\Users\Ron\Desktop\Test_1\image.jpg"
target = r"C:\Users\Ron\Desktop\Test_2\new_image.jpg"

shutil.move(original, target)

The file, with the new name, should now appear in the Test_2 folder.

Move a Directory using Python

So far, you have seen how to move a file in Python.

Alternatively, you may move a directory using this template (without specifying any file extension):

import shutil

original = r"original path of the directory\directory name"
target = r"target path to move the directory\directory name"

shutil.move(original, target)

For instance, let’s say that a new directory was added to the Test_1 location, where the directory name is my_folder.

Therefore, the following code can be used to move the directory to the Test_2 target location:

import shutil

original = r"C:\Users\Ron\Desktop\Test_1\my_folder"
target = r"C:\Users\Ron\Desktop\Test_2\my_folder"

shutil.move(original, target)

The directory would now appear under the target location.

You just saw how to move a file in Python using shutil.move. You may also want to check the following guide that explains how to copy a file in Python.

Leave a Comment