DEV Community

Cover image for Move all files from one folder to another with Python
Stokry
Stokry

Posted on

2 2

Move all files from one folder to another with Python

Hello, today I just want to show you how to make a simple Python script that will move all files from one folder to another. Also if you have a subfolder it will also be moved.

First off all we need to import import shutil module. This module offers a number of high-level operations on files and collections of files. In particular, functions are provided which support file copying and removal.
After that we need to import os. This module provides a portable way of using operating system dependent functionality. If you just want to read or write a file.

import shutil
import os

source_dir = 'C:\\Users\\Stokry\\Desktop\\ttt\\a' 
target_dir = 'C:\\Users\\Stokry\\Desktop\\ttt\\b'

file_names = os.listdir(source_dir)

for file_name in file_names:
    shutil.move(os.path.join(source_dir, file_name), target_dir)y
Enter fullscreen mode Exit fullscreen mode

This will remove all files from folder A to folder B. Also, you can make a job that will run this script periodically.

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Eliminate Context Switching and Maximize Productivity

Pieces.app

Pieces Copilot is your personalized workflow assistant, working alongside your favorite apps. Ask questions about entire repositories, generate contextualized code, save and reuse useful snippets, and streamline your development process.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay