Day #87 - Shutil Module in Python

Day #87 - Shutil Module in Python

ยท

3 min read

Introduction

Welcome to my 87th blog post on the Python coding journey. On day 87, I learned about a new module called 'Shutil' that help you to interface and automate files and directories in a very efficient manner. Let's dive into more details and understand the 'Shutil' module in Python in this blog post.

So let's get started......

Shutil Module

  • It provides a higher-level interface to work with files and directories.

  • The name "shutil" is short for shell utility.

  • Using Shutil Module we can automate tasks conveniently that are commonly performed on files and directories.

Functions of 'Shutil' Module

The following are some of the most commonly used functions in the shutil module:

  • shutil.copy(src, dst): It copies the file located at source (src) to a new location specified by destination (dst). If the destination already exists, then the original file is overwritten.

  • shutil.copy2(src, dst): It is similar to shutil.copy(src, dst), but it also preserves more metadata about the original file, such as the timestamp.

  • shutil.copytree(src, dst): This function recursively copies the directory located at src to a new location specified by dst. If the destination location already exists, the original directory will be merged with it.

  • shutil.move(src, dst): This function moves the file located to a new location. This function is equivalent to renaming a file in most cases.

  • shutil.rmtree(path): This function recursively deletes the directory located at path, along with all of its contents.

Example -

Here is the below example in which we take a look at the use of the 'Shutil' Module.

import shutil

# Copying a file
shutil.copy("src.txt", "dst.txt")

# Copying a directory
shutil.copytree("src_dir", "dst_dir")

# Moving a file
shutil.move("src.txt", "dst.txt")

# Deleting a directory
shutil.rmtree("dir")

In this way, we saw how we can use the shutil module to provides perform common file and directory-related tasks in Python.

Resources Used

You can watch the video of Day#87 by clicking on the below link ๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡

Conclusion

Thanks, guys for going through this blog post. On day 87, I learned about the 'shutil' module in Python. This can be used to automate tasks related to files and directories in Python. This module can be used as a powerful tool by any Python developer in their toolkit.

Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of Python coding. This is it for Day #87

See you in the next one.....


About Me

Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.

I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.

So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.


ย