How to move files using Python

share link

by Dejaswarooba dot icon Updated: May 9, 2023

technology logo
technology logo

Guide Kit Guide Kit  

We can move the files in Python using the shutil module. The shutil module is a part of the Python Standard Library. It provides various functions for high-level file operations. We can use this module to move single or many files from one place to another. We can use this module to copy and delete files and directories and work with archive files like zip and tar files. These functions provide a high-level interface for working with files and directories. We can use it in applications like file synchronization, backup & restore, and compression. Additionally, we can use the Pathlib module to handle paths in a more object-oriented way. 

Steps to move a file in Python: 

  • The first step in getting started is to import the shutil module. 
  • We can use the shutil.move() function to move the file from its present place to a new location. We can do it after importing the shutil module. 
  • The move() method has the following syntax: shutil.move(src, dst, copy_function=copy2) 
  • This method accepts two arguments: 
  • the original file location 
  • The destination file location. 
  • We can move the path to the source file or directory. We can give it as 'src', and the path to the destination file or directory is 'dst'. The 'copy_function' parameter is optional. It allows you to provide the function we can use to copy the file. 
  • You can use a loop to cycle through a list of file paths. We can call the shutil.move() method for each file if you want to move many files. 

Keep the file's location, extension, and system in mind when moving files. Furthermore, if the file already exists in the destination location. Then the shutil.move() will overwrite it. As a result, it is critical to ensure that the destination file does not already exist. It was critical to handle such circumstances. We can move the file by renaming it when supplying a different name for the destination file. We can do it by calling the shutil.move() method and specify a new file name in the destination path. 

To shutil, other modules such as os and glob can also handle files and directories in Python. The os module provides file handling and path manipulation functions. We can do it while this module provides pattern-matching functions for file collections. 

Source code

Before execution of the code

After execution of the code


Follow the steps carefully to get the output easily.

  • Install Visual Studio Code in your computer.
  • Open the folder in the code editor, copy and paste the above kandi code snippet in the python file.
  • Set the path of the source and destination folder in the variables path1 and fpath respectively.
  • Remove the line 'if f.endswith('.mp3')' - It is optional to specify the file type.
  • Run the code using the run command.

I hope you found this useful. I have added version information in the following sections.

I found this code snippet by searching for "how to move files in python" in kandi. You can try any such use case!


1. What is the shutil module, and how does it enable Python to move files? 

The shutil module is a standard Python library module. It provides a high-level interface for many file and directory operations. It supports copying, moving, and deleting files and directories. We can do it by working with archive formats like zip and tar. The shutil.move() function can move files using the shutil module. The move() function accepts two arguments: the source and destination file paths. 

2. What is an absolute and relative file path? When should we use it to move each file using Python? 

An absolute file path is a complete file path. It identifies the path of a file or directory beginning at the file system's root. A relative file path will help identify a file's location in its current directory. Moving the relative file path is appropriate if we find the file's location in its directory. When moving a file or directory in a fixed location on the file system, an absolute file path is usually the best. 

Environment tested

  1. This code had been tested using python version 3.8.0


  1. For any support on kandi solution kits, please use the chat
  2. For further learning resources, visit the Open Weaver Community learning page.