How to move and overwrite files and folders using Python?


The files are very important documents in our systems. In files we store our important data and moving these files is also an important task to organize the system. So we will learn how to move and also overwrite the files and folders using Python modules.

Understanding the Logic for the Problem

The problem at hand is that we have to create a code for moving and overwriting the files and folders in Python. For solving this problem we will import os and shutil libraries of Python. So with the help of these libraries we can move and overwrite the files and folders in our systems. And for moving and overwriting we will have to give the path of that particular file or folder.

What is the Purpose of Python’s Shutil and os Libraries ?

The Shutil module in python provides many high level processes on files and collections of files. This module mainly provides the tools to facilitate file removal and copying. The shutil module provides functions to use like move, copy, listdir, delete and so on.

The OS module of Python offers functions for adding and deleting the folders or directories and is used to retrieve the contents within the directories. It is also used to change the current directory and much more. So in simple terms we can say that this module offers to utilize operating system features. In this module functions like open, close, write are available.

Algorithm - For Moving the File

  • Step 1 − Import the necessary libraries like os and shutil.

  • Step 2 − Next, we’ll specify the path of source file and destination file, which is used to move from source folder to the destination folder.

  • Step 3 − After that we will check, if the file is already present then remove it from the destination folder.

  • Step 4 − And move the source file in the destination folder using shutil.

Example - For Moving the File

import os
import shutil

# Move a file
src_file = 'D:/folder/Source folder/file.txt'
dest_file = 'D:/folder/Destination folder/'

# Remove destination file if it exists
if os.path.exists(dest_file):
   os.remove(dest_file)

# Move the file
shutil.move(src_file, dest_file)

Output

Process finished with exit code 0

The above output shows that our file has been moved or replaced from the source folder to the destination folder.

Algorithm - For Moving the Folder

  • Step 1 − Import the libraries - os and shutil.

  • Step 2 − Next, we’ll specify the path of the source folder and destination folder, which will be utilised to move the folder from source to the destination folder.

  • Step 3 − Then after checking if the folder is already present in the destination folder then remove it.

  • Step 4 − And move the source folder in the destination folder using shutil.

Example - For Moving the Folder

import os
import shutil

# Move a folder (directory)
src_folder = 'C:/Users/Desktop/folder/Source folder'
dest_folder = 'C:/Users/Desktop/folder/Destination folder'

# Remove destination folder if it exists
if os.path.exists(dest_folder):
   shutil.rmtree(dest_folder)

# Move the folder
shutil.move(src_folder, dest_folder)

Output

Process finished with exit code 0

The above output means that your folder has been moved from the source to the destination folder.

Algorithm - For Overwriting the File and Folder

  • Step 1 − Import the libraries - os and shutil to overwrite the file and folder.

  • Step 2 − Next, we’ll specify the path of source folder/file and destination folder/file. This path will be used to move from the folder from source to the destination folder.

  • Step 3 − Then we will check if the file/folder is already present in the destination folder then remove it.

  • Step 4 − And move the source folder/file in the destination folder using shutil.

Example - For Overwriting the File and Folder

import shutil

# Overwrite a file
src_file = 'D:/My Folder/Source/my_file.txt'
dest_file = 'D:/My Folder/Destination/my_file.txt'

shutil.copy(src_file, dest_file)

# Overwrite a directory
src_folder= 'D:/source/My folder'
dest_folder = 'D:/destination/My folder'  # Overwrite the directory name folder

shutil.copytree(src_folder, dest_folder, dirs_exist_ok=True)

Output

Process finished with exit code 0

The above output means that your folder has been overwritten from the source to the destination folder.

Conclusion

We have learned the usage of shutil and os libraries of Python in this article. Moving a file or folder is easy with the help of these libraries. For moving or overwriting the file or folder only we will have to give the path of source and destination folder/file.

Updated on: 16-Oct-2023

475 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements