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

PHPz
Release: 2023-07-31 11:54:26
Original
1127 people have browsed it

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

In the Python programming language, the shutil module is a powerful tool that can be used for file and directory operations. It provides a simple and easy-to-use set of functions for copying, moving, deleting files and directories, and creating directories.

This article will introduce the use of shutil module in the form of code examples.

1. Copy files

Use the shutil.copy() function in the shutil module to copy files. This function accepts two parameters: the path to the source file and the path to the destination file.

The following is a simple sample code that demonstrates how to use shutil.copy() function to copy files:

import shutil

# 源文件路径
src_file = 'path/to/source_file.txt'

# 目标文件路径
dst_file = 'path/to/destination_file.txt'

shutil.copy(src_file, dst_file)
Copy after login

In the above code, src_file is the source to be copied The path of the file, dst_file is the path of the target file. Executing shutil.copy(src_file, dst_file) will copy the source file to the target file.

2. Move files

Use the shutil.move() function in the shutil module to move files. This function accepts two parameters: the path to the source file and the path to the destination file.

The following is a simple sample code that demonstrates how to use shutil.move() function to move files:

import shutil

# 源文件路径
src_file = 'path/to/source_file.txt'

# 目标文件路径
dst_file = 'path/to/destination_file.txt'

shutil.move(src_file, dst_file)
Copy after login

In the above code, src_file is the source to be moved The path of the file, dst_file is the path of the target file. Executing shutil.move(src_file, dst_file) will move the source file to the target file.

3. Delete files

Use the shutil.remove() function in the shutil module to delete files. This function accepts one parameter: the path of the file to be deleted.

The following is a simple sample code that demonstrates how to use shutil.remove() function to delete files:

import shutil

# 要删除的文件路径
file = 'path/to/file.txt'

shutil.remove(file)
Copy after login

In the above code, file is the file to be deleted path of. Executing shutil.remove(file) will delete the specified file.

4. Create a directory

Use the shutil.mkdir() function in the shutil module to create a new directory. This function accepts one parameter: the path to the directory to be created.

The following is a simple sample code that demonstrates how to use shutil.mkdir() function to create a directory:

import shutil

# 要创建的目录路径
dir = 'path/to/new_directory'

shutil.mkdir(dir)
Copy after login

In the above code, dir is the directory to be created path of. Executing shutil.mkdir(dir) will create a new directory under the specified path.

Summary

The shutil module is a powerful file and directory manipulation tool in Python 3.x. This article introduces some of its features and gives code examples. By using the shutil module, we can easily copy, move, delete files and directories, and create directories. Developers can flexibly use these functions according to their own needs to improve their programming efficiency.

The above is the detailed content of How to use shutil module for file and directory operations in Python 3.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!