How to use shutil module for file and directory operations in Python 2.x

WBOY
Release: 2023-07-30 16:44:05
Original
1238 people have browsed it

How to use the shutil module for file and directory operations in Python 2.x

Introduction:
In Python, the shutil module is a powerful file and directory operation tool. It provides a set of simple and easy-to-use functions that can be used to copy, move, and delete files, as well as create, delete, and traverse directories.

  1. Working with files
    Copying a file from one location to another is a common task, and the shutil module provides convenient functions to achieve this goal. The following is an example:
import shutil

# 复制文件
shutil.copy('source_file.txt', 'destination_file.txt')
Copy after login

In the above code example, we use the shutil.copy() function to copy the file named source_file.txt to the file named destination_file.txt file. If the target file does not exist, this function will automatically create one.

  1. Move files
    Like copying files, moving files is also a common operation. We can use shutil.move() function to achieve this.
import shutil

# 移动文件
shutil.move('old_location.txt', 'new_location.txt')
Copy after login

In the above example, we used the shutil.move() function to move the file named old_location.txt to the file named new_location.txt s position. If the target file already exists, this function will overwrite the file.

  1. Delete files
    If we want to delete a file, we can use the shutil.remove() function in the shutil module.
import shutil

# 删除文件
shutil.remove('file_to_delete.txt')
Copy after login

In the above code example, we used the shutil.remove() function to delete the file named file_to_delete.txt.

  1. Processing directories
    The shutil module can not only process files, but also directories. The following are several examples of commonly used directory operation functions:
  • Create a directory: The shutil module provides the shutil.mkdir() function to create a new directory.
import shutil

# 创建目录
shutil.mkdir('new_directory')
Copy after login

In the above code example, we used the shutil.mkdir() function to create a new directory named new_directory.

  • Copy directory: The shutil.copytree() function in the shutil module can copy a directory and its contents to another location.
import shutil

# 复制目录
shutil.copytree('source_directory', 'destination_directory')
Copy after login

In the above example, we used the shutil.copytree() function to copy the source_directory directory and its contents to the destination_directory directory.

  • Move directory: The shutil module provides the shutil.move() function to move a directory.
import shutil

# 移动目录
shutil.move('old_directory', 'new_directory')
Copy after login

In the above code example, we used the shutil.move() function to move a directory named old_directory to a directory named new_directory s position.

  • Delete a directory: The shutil.rmtree() function in the shutil module can delete a directory and its contents.
import shutil

# 删除目录
shutil.rmtree('directory_to_delete')
Copy after login

In the above code example, we used the shutil.rmtree() function to delete the directory named directory_to_delete and its contents.

Summary:
The shutil module is a very convenient tool in Python that can be used to perform file and directory operations. Whether it is copying, moving, deleting files or creating or deleting directories, the shutil module provides simple and easy-to-use functions. I hope the above sample code can help you better understand and use the shutil module.

The above is the detailed content of How to use shutil module for file and directory operations in Python 2.x. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!