Python delete non-empty folder instance

不言
Release: 2018-04-27 10:42:35
Original
1913 people have browsed it

The following is an example of deleting a non-empty folder in python. It has a good reference value and I hope it will be helpful to everyone. Let's take a look together

Generally, when deleting files, use the os library, and then use os.remove(path) to complete the deletion. If you delete an empty folder, you can use os.removedirs(path),

But if you need to delete the entire folder, and the folder is not empty, an error will be reported when using os.removedirs(path). At this time, you can use the shutil library, which is a built-in library of Python and is a library for files and The library for advanced folder operations can complement the os library to complete some operations, such as copying the entire folder, moving folders, renaming files, etc.

import os import shutil os.remove(path) #删除文件 os.removedirs(path) #删除空文件夹 shutil.rmtree(path) #递归删除文件夹
Copy after login

Related recommendations:

Introduction to file opening and closing operation commands in python

The above is the detailed content of Python delete non-empty folder instance. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
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!