VOOZH about

URL: https://www.geeksforgeeks.org/python/how-to-move-all-files-from-one-directory-to-another-using-python/

⇱ How to move all files from one directory to another using Python ? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to move all files from one directory to another using Python ?

Last Updated : 23 Jul, 2025

In this article, we will see how to move all files from one directory to another directory using Python.  In our day-to-day computer usage we generally copy or move files from one folder to other, now let's see how to move a file in Python:

This can be done in two ways:

  • Using os module.
  • Using shutil module.

Source and Destination Folder Setup Before Running Script:

👁 source folder and destination folder layout
Source and Destination folder placements
👁 Source folder contents
Text files inside Source Folder
👁 Destination folder before running script
Destination Folder - before

Using os.rename() method move Files in Python

rename() method takes two arguments first one is the source path and the second one is the destination path, the rename function will move the file at the source path to the provided destination.

Code:

Using shutil.move() method move Files in Python using the 

The shutil.move() method takes two arguments first one is the complete source path and the second one is the destination path (including the file/folder name to move), the move function will move the file from source to the destination.

Output:

👁 How to move all files from one directory to another using Python
Destination Folder - after

Move Files Matching specific pattern

Here we are trying to move all files having name patterns *_A_* to our destination directory. Using glob module in Python, we can easily do this in a single line, by defining a pattern for the file name.

Output:

Files to move ['/home/tuhingfg/Documents/source/type_A_4.txt', '/home/tuhingfg/Documents/source/type_A_5.txt', '/home/tuhingfg/Documents/source/type_A_2.txt', '/home/tuhingfg/Documents/source/type_A_3.txt']
Moved /home/tuhingfg/Documents/source/type_A_4.txt -> /home/tuhingfg/Documents/destination/type_A_4.txt
Moved /home/tuhingfg/Documents/source/type_A_5.txt -> /home/tuhingfg/Documents/destination/type_A_5.txt
Moved /home/tuhingfg/Documents/source/type_A_2.txt -> /home/tuhingfg/Documents/destination/type_A_2.txt
Moved /home/tuhingfg/Documents/source/type_A_3.txt -> /home/tuhingfg/Documents/destination/type_A_3.txt

Destination folder after executing script:

👁 How to move all files from one directory to another using Python
Destination folder containing files of specific naming patterns
Comment
Article Tags:
Article Tags: