TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Shutil Module in Python
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Python BasicPython Tutorials

Shutil Module in Python

Last updated: Nov 23, 2023 11:24 am
By Meenakshi Agarwal
Share
3 Min Read
Python Shutil Module with Examples
Python Shutil Module with Examples
SHARE

From this tutorial, you will learn about the Python Shutil module. It facilitates operations on files which makes it one of the essential modules to learn. Read on to know more.

Contents
What is the Shutil module?How does the Python Shutil module work?Program Exampleswhich() to show the directory of an execopystat() on a filemove() to move a filecopy() to copy a filermtree() to delete files from dirs and subdirs

Note: The syntax used in the below section is for Python 3. You may change it to use a different version of Python.

Python Shutil Module

Must Read – 9 Ways to Copy a File in Python

What is the Shutil module?

The shutil in Python is a module that offers several functions to deal with operations on files and their collections. It provides the ability to copy and remove files.

In a way, it is similar to the OS Module; however, the OS Module does have functions dealing with collections of files.

How does the Python Shutil module work?

The syntax to use the shutil module is as follows:

import shutil
shutil.submodule_name(arguments)

Some examples of its functions are copytree(), make_archive(), which(), rmtree(), copy(), copy2(), copystat(), move(), etc.

In the next section, you will learn to use the few common functions frequently used in everyday Python programming.

Program Examples

which() to show the directory of an exe

Here is a simple program to demonstrate which() method.

import shutil

shutil.which("python")

shutil.which("python3")

shutil.which("ls")

The output will come as:

Python Shutil Module - Which Method

copystat() on a file

Check another program to use the copystat() function.

import shutil

shutil.copystat("file_path", "path_to_file_where_you_want_to_copy")

The output will come as:

Python Shutil Module - CopyStat1
CopyStat2

move() to move a file

Below is the program to use move() to relocate a file from one location to another.

import shutil

shutil.move("file_path", "path_to_where_you_want_to_move_file")

The output will come as:

Python Shutil Module - Move1
Move2

copy() to copy a file

Here is the program to copy a file from one location to another using copy().

import shutil

shutil.copy("file_path", "path_to_directory_where_you_want_to_copy_to")

The output will come as:

Python Shutil Module - Copy1
Copy2

rmtree() to delete files from dirs and subdirs

This function removes all the contents of a directory recursively.

import shutil

shutil.rmtree("dir_path")

The output will come as:

import shutil
targetdir = '/tmp/reports/';
# Clear a directory using rmtree()
try:
    shutil.rmtree(targetdir)
except:
    print("Operation failed!")

Summary

Here are some key usages and important points of the shutil module in Python, summarized in point form:

  • The shutil module offers high-level file operations, such as copying, moving, deleting, and creating files and directories.
  • It is a part of the Python standard library, so you do not need to install any additional modules to use it.
  • It is a powerful tool for automating file management tasks.
  • It should be used with caution, as it can delete files and directories without warning.

Here are some additional important points to keep in mind:

  • The shutil module does not support all file operations. For example, it cannot rename files in Python or change their permissions.
  • The shutil module is not thread-safe. This means that you should not use it in a multithreaded application.
  • The shutil module can be used to perform dangerous operations, such as deleting files and directories. You should use it with caution.

I hope this helps! Let me know if you have any other questions.

Best,

TechBeamers

You Might Also Like

How to Connect to PostgreSQL in Python

Generate Random IP Address (IPv4/IPv6) in Python

Python Remove Elements from a List

How to Use Extent Report in Python

10 Python Tricky Coding Exercises

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article Python Static method with examples Python Static Method Explained
Next Article Shallow Copy vs. Deep Copy in Python Shallow Copy vs. Deep Copy in Python

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x