Is there any way to remove a directory and it’s contents in the PathLib module? With path.unlink()
it only removes a file, with path.rmdir()
the directory has to be empty. Is there no way to do it in one function call?
Python pathlib change directory We go inside another directory with os' chdir . #!/usr/bin/python from pathlib import Path from os import chdir path = Path('..') print(f'Current working directory: {path. cwd()}') chdir(path) print(f'Current working directory: {path. cwd()}') chdir('..')
The pathlib module replaces many of these filesystem-related os utilities with methods on the Path object. Notice that the pathlib code puts the path first because of method chaining!
The Pathlib module in Python deals with path related tasks, such as constructing new paths from names of files and from other paths, checking for various properties of paths and creating files and folders at specific paths.
The pathlib library does not have its own recursive directory removal function ( pathlib rmdir only removes empty directories), so this is the way to delete a directory and all of its contents with a pathlib.Path:
Remove a directory recursively In Python the “shutil” module provides the function shutil.rmtree (path) to remove all the contents of a directory.
The OS module in Python provides methods to interact with the Operating System in Python. The remove () method in this module is used to remove/delete a file path. Import the shutil module and pass directory path to shutil.rmtree ('path') function to delete a directory and all files contained in it.
Again, for removing an empty directory, you may use the pathlib’s rmdir () function. For example: You may perform many high-level operations on files/directories by using shutil module. Included in these operations are enabling to delete files and subdirectories by using rmtree function .
As you already know, the only two Path
methods for removing files/directories are .unlink()
and .rmdir()
and neither does what you want.
Pathlib is a module that provides object oriented paths across different OS's, it isn't meant to have lots of diverse methods.
The aim of this library is to provide a simple hierarchy of classes to handle filesystem paths and the common operations users do over them.
The "uncommon" file system alterations, such as recursively removing a directory, is stored in different modules. If you want to recursively remove a directory, you should use the shutil
module. (It works with Path
instances too!)
import shutil import pathlib import os # for checking results print(os.listdir()) # ["a_directory", "foo.py", ...] path = pathlib.Path("a_directory") shutil.rmtree(path) print(os.listdir()) # ["foo.py", ...]
Here's a pure pathlib implementation:
from pathlib import Path def rm_tree(pth): pth = Path(pth) for child in pth.glob('*'): if child.is_file(): child.unlink() else: rm_tree(child) pth.rmdir()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With