Move all files and specific files from one directory to another using shutil module in Python

In this tutorial, we will learn about moving files from one directory to another using Python’s shutil module.

Here we make use of shutil.move() method to move files from source directory to destination directory.

Necessary modules:

  • shutil
  • os

Let us have 2 cases of data movement:

  • Moving all the contents in source directory to destination directory.
  • Moving only specific files from source directory to destination directory.

You can also check: Delete files using send2trash module in Python

How to recursively copy a directory in Python

move all files from one directory to another in Python

import os
import shutil

'''
shutil.move() method syntax: shutil.move(source, destination, copy)
source: string representing path of the source 
destination: string representing path of destination 

'''

print("Contents of source and destination before moving:")
# path of the source from where data is to be moved
source = r"C:\Users\User\Desktop\Source"

# path of the destination where the data is to be copied
destination = r"C:\Users\User\Desktop\Destination"

# contents of source path
print(os.listdir(source))

# contents of source path before moving
print(os.listdir(destination))

# moving files
shutil.move(r"C:\Users\User\Desktop\Source\CodeSpeedy",destination)  # moving the CodeSpeedy folder from source directory to destination directory

# contents of directories after moving files
print("Contents of source and destination after moving CodeSpeedy folder:")
print(os.listdir(r"C:\Users\User\Desktop\Source"))
print(os.listdir(destination))

Output:

Contents of source and destination before moving:
['Code Composer Studio 10.1.1.lnk', 'CodeSpeedy', 'PyCharm Community Edition 2020.1 x64.lnk']
['Arduino.lnk', 'Multisim 14.2.lnk']
Contents of source and destination after moving CodeSpeedy folder:
['Code Composer Studio 10.1.1.lnk', 'PyCharm Community Edition 2020.1 x64.lnk']
['Arduino.lnk', 'CodeSpeedy', 'Multisim 14.2.lnk']

move specific files from one directory to another in Python

Here we want to move all the text files i.e. “.txt”  files present in CodeSpeedy folder of source directory and its sub directories to the destination directory :

Pictorial representation of source directory:

move all files from one directory to another in Python

Here in this case we will move specifically move only the text files in CodeSpeedy folder and its sub folders.

import os
import shutil

'''
shutil.move() method syntax: shutil.move(source, destination, copy)
source: string representing path of the source 
destination: string representing path of destination 
os.walk() returns a list of main directory's subdirectory's and files
'''

print("Contents of source and destination before moving:")
# path of the source from where data is to be moved
source = r"C:\Users\User\Desktop\Source\CodeSpeedy"

# path of the destination where the data is to be copied
destination = r"C:\Users\User\Desktop\Destination"

# contents of source path
for root, dir, files in os.walk(source):
    print(root)
    print(dir)
    print(files)

# contents of source path before moving
print(os.listdir(destination))

# moving text files
for root, dir, files in os.walk(source):
    for file in files:
        if ".txt" in file:  # checking if the file is a text file by looking for .txt extension in the name of the file
            shutil.move(os.path.join(root, file), destination)
            # os.path.join method is used to join the root path string and file name string

# contents of directories after moving files
print("Contents of destination after moving:")

# contents of destination path after moving text files
print(os.listdir(destination))

Output:

Contents of source and destination before moving:
C:\Users\User\Desktop\Source\CodeSpeedy
['Subfolder1', 'Subfolder2']
['text1.txt']
C:\Users\User\Desktop\Source\CodeSpeedy\Subfolder1
['subfolder1_1']
['stopwatch_stop.JPG', 'text2.txt']
C:\Users\User\Desktop\Source\CodeSpeedy\Subfolder1\subfolder1_1
[]
['text3.txt']
C:\Users\User\Desktop\Source\CodeSpeedy\Subfolder2
['Subfolder2_1']
['text4.txt']
C:\Users\User\Desktop\Source\CodeSpeedy\Subfolder2\Subfolder2_1
[]
['stopwatch pause.JPG', 'text5.txt']
['Arduino.lnk', 'Multisim 14.2.lnk']
Contents of destination after moving:
['Arduino.lnk', 'Multisim 14.2.lnk', 'text1.txt', 'text2.txt', 'text3.txt', 'text4.txt', 'text5.txt']

Leave a Reply

Your email address will not be published.