Created by RichieHindleLinked to 22.8m issues across 146 teams
In Python, there are several ways to delete a file or folder.
The os.remove()
function can be used to delete a file. This function takes the path of the file as an argument and deletes it.
The os.rmdir()
function can be used to delete an empty directory. This function takes the path of the directory as an argument and deletes it.
The shutil.rmtree()
function can be used to delete a directory and all its contents. This function takes the path of the directory as an argument and deletes it and all its contents.
In Python 3.4 and later, the pathlib
module provides a Path
object which exposes several instance methods for deleting files and folders. The Path.unlink()
method can be used to delete a file or symbolic link. The Path.rmdir()
method can be used to delete an empty directory.
To delete a file or folder in Python, use the appropriate function or method depending on the version of Python and the type of file or folder you are deleting.
Contributor
Teams
146