Move all files from one folder to another with Python

Stokry - Sep 25 '20 - - Dev Community

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.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .