In this article we will explore how to move files from one directory to another using Python.

Table of Contents



Introduction

This article aims to provide a comprehensive guide on how to move files from one directory to another using Python.

Whether you need to move individual files, multiple files, an entire directory, or automate file moving tasks, Python has the tools you need.

In this tutorial we will work with the os and shutil (“shell utility”) modules. os module provides a portable way of using operating system dependent functionality, and shutil module that provides a wide range of file operations. Both modules are a part of the standard library, so there is nothing additional to install.


Sample files

Here are the 3 sample files we will use in this tutorial:

and we will place them in the sample_files folder. We will also create an empty folder and call it new_folder. Now your directory with the code file should look like this:


Move a single file

In this section, we’ll explore how to move a single file from one directory to another using Python.

This is one of the simplest operations when dealing with file management tasks, and Python makes it straightforward with the shutil module:


#Import the required dependencies
import os
import shutil

#Define path to folder where file is located
source_directory = 'sample_files/'

#Define path to folder to which the file will be moved
destination_directory = 'new_folder/'

#Define the file name
file_name = 'sample_file1.txt'

#Get the relative full path to file
source_path = os.path.join(source_directory, file_name)

#Create the new path to file, to which it will be moved
destination_path = os.path.join(destination_directory, file_name)

#Move file from one directory to another
shutil.move(source_path, destination_path)
print(f'File moved from {source_path} to {destination_path}')

You should see the following message printed:

File moved from sample_files/sample_file1.txt to new_folder/sample_file1.txt

and you should see the moved file in the destination directory:

Note: unlike the copy task, in case of moving the file, it doesn’t get duplicated in the new directory, rather than it’s moved from one directory to another directory. You can think of it as drag & drop, whereas when we copy the file using Python it’s like we copy & paste the file.


Move multiple files

In this section, we’ll explore how to move multiple files from one directory to another using Python.

This is especially useful when you need to move multiple files from a source folder to a destination folder.

The code will be 90% the same as in the previous section, except now we will iterate through a list of files and move them one by one.

Note: In previous section we moved one file (sample_file1.txt) from sample_files to new_folder. Let’s move it back from new_folder to sample_files, so we have the original file structure:


Now, to move multiple files, we are going to create a list of file names that we want to copy and then use a loop to iterate through the list, generate file paths, and move each file one at a time:


#Import the required dependencies
import os
import shutil

#Define path to folder where file is located
source_directory = 'sample_files/'

#Define path to folder to which the file will be moved
destination_directory = 'new_folder/'

#Define the file names
file_names = ['sample_file1.txt', 'sample_file2.txt']

#Iterate over files
for file_name in file_names:

    #Get the relative full path to file
    source_path = os.path.join(source_directory, file_name)

    #Create the new path to file, to which it will be moved
    destination_path = os.path.join(destination_directory, file_name)

    #Move file from one directory to another
    shutil.move(source_path, destination_path)
    print(f'File moved from {source_path} to {destination_path}')

You should see the following message printed:

File moved from sample_files/sample_file1.txt to new_folder/sample_file1.txt
File moved from sample_files/sample_file2.txt to new_folder/sample_file2.txt

and you should see the moved files in the destination directory:


Move a directory

In this section, we’ll learn how to move an entire directory, including its subdirectories and files, from one location to another using Python.

This operation is useful when you need to duplicate an entire folder structure along with its contents.

Note: In previous section we moved multiple files (sample_file1.txt and sample_file2.txt) from sample_files to new_folder. Let’s move it back from new_folder to sample_files, so we have the original file structure:

The code in this section will be 99% similar to the code in the previous section.

To move an entire directory from one location to another (including all of its subdirectories and files), we will need the list of all files and folders present in the source directory.

Using os.listdir() function we can generate a list of all files and folders, and then move it one by one:


#Import the required dependencies
import os
import shutil

#Define path to folder where file is located
source_directory = 'sample_files/'

#Define path to folder to which the file will be moved
destination_directory = 'new_folder/'

#Define the file names
file_names = os.listdir(source_directory)

#Iterate over files
for file_name in file_names:

    #Get the relative full path to file
    source_path = os.path.join(source_directory, file_name)

    #Create the new path to file, to which it will be moved
    destination_path = os.path.join(destination_directory, file_name)

    #Move file from one directory to another
    shutil.move(source_path, destination_path)
    print(f'File moved from {source_path} to {destination_path}')

You should see the following message printed:

File moved from sample_files/sample_file1.txt to new_folder/sample_file1.txt
File moved from sample_files/sample_file2.txt to new_folder/sample_file2.txt
File moved from sample_files/sample_file3.txt to new_folder/sample_file3.txt

and you should see the moved files in the destination directory:


Conclusion

In this article explored how to move files from one directory to another using Python with os and shutil modules.

Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Python for File Handling tutorials.