Shutil Module in PythonIn this tutorial, we will learn about the Shutil module in Python. We will discuss how we can perform the high-level file operation such as creating a new copy file and archive it and copy content one file to another file using the Python script. Let's have a basic introduction of Shutil module. Python Shutil ModulePython shutil module provides the facility to perform the high-level file operation. It can operate with the file object and offers us the ability of copy and remove the files. It handles the low-level semantic such creating and closing file objects after performing all operations. Working of Shutil ModulePython shutil module comes with the many built-in methods. We will explore a few important methods. To start working with this module, first we need to import it in our current Python file. Copy FilesThis module provides the copy() function which is used to copy a data from one file to another. The files must be in the same directory and destination file must be writable. Let's understand the following syntax. Syntax- Parameter: In the above syntax -
Let's understand the following example. Example - Output: Empty Folder: [] File Copied Name: ['testcompare.py'] Explanation - The copy() function takes directory name as an argument. Here the metadata is not copied, the copied file will be considered as the freshly created file. This method also cloned the all permission of the file. One thing to note is that if the destination file already exists, it will be replaced with the source file. Let's see another example. Example - 2 if a destination is a directory Output: D:\Python Project\NewFile\hello.txt As we have mentioned that, the copy() function doesn't copy the metadata. But, we will use the copy2() function which allows us to copy the file including its metadata. Example - 3: Error handling while using copy method Output: Source and destination represents the same file. The copy2() FunctionThis function is the similar to the copy() function. It can also copy the content of one file to another but the only difference is it can preserve the file's metadata. Let's understand the following syntax. Syntax: Parameter: In the above syntax -
Let's understand the following example. Example - Output: os.stat_result(st_mode=33206, st_ino=562949953459285, st_dev=3029671014, st_nlink=1, st_uid=0, st_gid=0, st_size=17, st_atime=1622815671, st_mtime=1622705607, st_ctime=1622705607) After copying file os.stat_result(st_mode=33206, st_ino=562949953459287, st_dev=3029671014, st_nlink=1, st_uid=0, st_gid=0, st_size=17, st_atime=1622815748, st_mtime=1622705607, st_ctime=1622706243) D:\Python Project\NewFile\hello.txt The shutil.copyfile() FunctionThis method is used to copy the content of the source file to the destination file expect the metadata. Source and destination must have a file and destination file must provide the write permission. If there is destination file already present then it will be replaced by the new file otherwise create new file. Let's see the following syntax. Syntax: Parameters: In the above syntax -
Let's understand the following example. Example - Output: D:\Python Project\NewFile\hi.txt The shutil.copytree() FunctionThis method is used to replicate the complete directory. It copies an entire directory tree rooted at source to the destination directory. The destination directory must not already present. Let's see the following syntax. Syntax: Parameters: In the above syntax:
Example - Output: Destination path: D:\Python Project\NewFolder The shutil.rmtree()This method is used to delete the complete directory tree. Let's see the following syntax. Syntax: Parameter- In the above syntax -
Let's understand the following example - Example - The above code will remove the given directory. The shutil.which() FunctionThe shutil.which() function is used to get the path of an executable application that would be run if the given cmd was called. It finds the file in the given path. Let's see the following syntax. Syntax: Parameters In the above syntax -
Let's understand the following example. Example - Output: C:\Python\python.EXE It will find the given file in the computer, if file is found it returns the path of the file otherwise returns None. Next TopicPython epoch to Datetime |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India