How to Move a File or Directory in Python (with examples)

You may use the following template to move a file in Python:

import shutil

original = r'original path where the file is currently stored\file name.file extension'
target = r'target path where the file will be moved\file name.file extension'

shutil.move(original,target)

Alternatively, you can use this template to move a directory:

import shutil

original = r'original path where the directory is currently stored\directory name'
target = r'target path where the directory will be moved\directory name'

shutil.move(original,target)

Let’s now review some examples with the steps to move your file or directory in Python.

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, I stored a CSV file in a folder called Test_1:

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

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

How to Move a File or Directory in Python

Step 2: Capture the Target Path

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

In our example, I’ll be moving the CSV file to a folder called Test_2:

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

Capture the target path

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 where the file is currently stored\file name.file extension'
target = r'target path where the file will be moved\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:

import shutil

original = r'C:\Users\Ron\Desktop\Test_1\my_csv_file.csv'
target = r'C:\Users\Ron\Desktop\Test_2\my_csv_file.csv'

shutil.move(original,target)

Once I ran the code in Python, the CSV file moved to the Test_2 folder:

How to Move a File or Directory in Python

Rename the File when Moving it

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

For example, I added a new JPG file into the Test_1 folder (where the file name is image):

Example of JPG image

I then applied the following code 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, will now appear in the Test_2 folder:

Move a File Python

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 where the directory is currently stored\directory name'
target = r'target path where the directory will be moved\directory name'

shutil.move(original,target)

For instance, I added a new directory to the Text_1 location, where the directory name is ‘my_folder’

Folder within a folder

I then used the following code 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 will now appear under the target location:

Move Directory in Python

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.