Skip to content

How to Move Files in Python (os, shutil)

How to Move Files in Python (os, shutil) Cover Image

Automating tasks such as moving files or renaming files is an essential skill for any Python developer. Because Python is a general-purpose programming language, it offers many possibilities for working with and automating file workflows.

In this tutorial, you’ll learn how to use Python to move files, both a single file and multiple files, using the os, shutil, and Pathlib libraries. By the end of this tutorial, you’ll have learned the following:

  • How to understand the shutil.move(), os.rename(), and pathlib.Path.rename() functions
  • How to use Python to move a single file to a new directory
  • How to use Python to move multiple files to a new directory
  • How to move files based on a condition, such as a file extension

Quick Answer: Move a File with Python

How can you move a file with Python?

The simplest way to move a file with Python is to use the shutil.move() function. The function takes a file path and the destination of where you want to move the file to. For example, you can use shutil.move(‘old_location.txt’, ‘new_directory) to move a file.How to move a file with Python shutil.move

Understanding the Functions to Move Files With Python

There are three main functions that you can use to move files using Python:

  1. os.rename()
  2. shutil.move()
  3. pathlib.Path.rename()

While two of the functions above are named in such a way that they make it seem like they only rename files, they can actually be used to move files as well.

Understanding the os.rename() Function

Python comes with a built-in function, rename(), as part of the os library. As the name implies, the function is used to rename a file. However, it can also be used to move files! Let’s take a look at what makes up the function:

# Understanding the Python os.rename() Function
os.rename(
   src, 
   dst, 
   *, 
   src_dir_fd=None, 
   dst_dir_fd=None
)

From the code block above, you can see that the function accepts four arguments, two of which are optional. In this tutorial, we’ll focus on the first and second positional arguments in order to use them to move files.

Understanding the shutil.move() Function

The shutil.move() function is a dedicated function for moving files. Because of this, it tends to be my preferred method of moving files. This is because it makes it clear that the intention of your code is to move a file, rather than rename it.

Let’s take a look at what parameters the function has to offer:

# Understanding the shutil.move() Function
shutil.move(
   src, 
   dst, 
   copy_function=copy2
)

For the purposes of this tutorial, we’ll focus again on just the first two. These allow you to specify which file to move and where to move it to. Under the hood, the shutil.move() library generally uses the os.rename() function.

Understanding the pathlib.Path.rename() Method

The Pathlib library provides an object-oriented wrapper for working with paths and files. The .rename() method can also be used to move files, despite being called rename. Let’s take a look at the functions that make up the method:

# Understanding the pathlib.Path.rename() Method
Path.rename(target)

We can see that the method takes only a single argument – the destination. In this case, it allows us to pass the path to where we want to move a file, including its filename.

Now that you have a strong understanding of how to use the three functions, let’s dive into how to use them to move a file.

How to Move a Single File with Python

In this section, we’ll explore how to use the three libraries to move a file using Python. We’ll dive into more detail in this section but then rely only on the shutil.move() function for the remainder of the tutorial. The processes are largely the same, but feel free to leave a comment if you run into any issues.

For all of the examples, let’s imagine that we have a file in a folder at '/Users/datagy/file.txt'. We want to move the folder to the folder '/Users/datagy/new_folder'.

How to Move a Single File with Python Using os.rename()

To use the os.rename() function to move a file to a directory, you can simply pass in the original file path and the new file path. If you want to rename the file while moving it, that’s possible. However, let’s keep the name the same for this tutorial and simply move the file:

# How to Move a File with os.rename()
import os

old_file = '/Users/datagy/file.txt'
destination = '/Users/datagy/new_folder/file.txt'

os.rename(old_file, destination)

In the example above we passed in the original file path and the destination file path. In the destination, we had to also pass in the filename. In this case, we kept the filename the same, in order to not rename the file. However, if you changed the filename, Python would move and rename the file.

It’s important to note here that if a file already exists on that path, it will be overwritten.

How to Move a Single File with Python Using shutil.move()

The shutil.move() function behaves slightly differently than the function above. Instead of passing in the filename of the destination, you can simply pass in the directory that you want to move your file to.

Let’s take a look at what this looks like:

# How to Move a File with shutil.move()
import shutil

old_file = '/Users/datagy/file.txt'
destination = '/Users/datagy/new_folder/'

shutil.move(old_file, destination)

In the example above, we passed in the file that we want to move and the directory to which we want to move it. You could pass in the filename path as well, which would accomplish the same thing. This is, because, shutil actually uses the os library under the hood.

How to Move a Single File with Python Using pathlib.Path.rename()

Finally, let’s take a lot at how to move a file using the pathlib.Path.rename() method. Because pathlib provides an object-oriented wrapper for working with paths, we use a method to move the file, rather than a function.

Let’s see what this looks like:

# How to Move a File with pathlib.Path.rename()
import pathlib

old_file = pathlib.Path('/Users/datagy/file.txt')
destination = pathlib.Path('/Users/datagy/new_folder/file.txt')

old_file.rename(destination)

In the example above, we use Path objects for both the original file path and the destination. We can then use the .rename() method and pass in the destination of where we want to move the file.

How to Move Multiple Files with Python

In order to move multiple files with Python, we can use any of the functions you learned above. In order to keep things simple, let’s focus on the shutil.move() function.

In order to move multiple files with Python, we can actually simply use a for loop to iterate over a list of files. In this way, we actually move one file at a time, but use the for loop to repeat this for all files in a folder.

Let’s see how we can use Python to move multiple files:

# How to Move Multiple Files with Python
import os
import shutil

dir = '/Users/datagy/old'
destination = '/Users/datagy/new_folder'

for file in os.listdir(dir):
    file_path = os.path.join(dir, file)
    shutil.move(file_path, destination)

In the example above, we first create variables for our original directory and the destination folder. We then loop over all of the files in a directory using the os.listdir() function.

For each file, we create the full path to the original file using the os.path.join() function and then use the move() function to move the files to the destination.

How to Move Files Conditionally in Python

In this final section, you’ll learn how to move files conditionally using Python. For example, you may only want to move pictures from a folder into another folder, rather than all files.

We can make this process easy by filtering our list of files based on their extensions. Once we know the extensions, we can use Python if-else statements to control the flow of our program. Let’s see how we can move files conditionally using Python:

# How to Move Files Conditionally with Python
import os
import shutil

dir = '/Users/datagy/old'
destination = '/Users/datagy/new_folder'
extension = '.jpg'

for file in os.listdir(dir):
    if os.path.splitext(file)[-1] == extension:
        shutil.move(os.path.join(dir, file), destination)

Let’s break down what we’re doing in the code block above:

  1. We declare variables for the directory, the destination, and the extension we want to move
  2. We then loop over the files in the directory to see if the extension is equal to the extension we want to move. We get the extension by accessing the last value in the result of splitting the text.
  3. If the file meets our condition, we move it using the shutil.move() function

Similarly, you could use this process to move files of a certain size.

Conclusion

In this tutorial, you learned how to use Python to move a file or multiple files. You learned how to use the shutil.move(), os.rename(), and pathlib.Path.rename() functions to move files. From there, you walked through some examples of using these functions to move a single file. You then learned how to move all files in a directory to a new directory. Finally, you learned how to move files based on conditions, such as a file’s extension.

Additional Resources

To learn more about related topics, check out the tutorials below:

Leave a Reply

Your email address will not be published.