Home > Article > Backend Development > Python delete non-empty folder instance
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) #递归删除文件夹
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!