Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PathLib recursively remove directory?

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?

like image 239
Jasonca1 Avatar asked May 05 '18 07:05

Jasonca1


People also ask

How do I change directory in Python Pathlib?

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('..')

Does Pathlib replace os path?

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!

What does Pathlib path do?

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.

How to delete all contents of a directory with pathlib?

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:

How do I remove a directory recursively in Python?

Remove a directory recursively In Python the “shutil” module provides the function shutil.rmtree (path) to remove all the contents of a directory.

How to remove/delete a file path in Python?

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.

How to remove an empty directory from a path in Linux?

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 .


2 Answers

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", ...] 
like image 136
Taku Avatar answered Oct 09 '22 10:10

Taku


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() 
like image 43
MatteoLacki Avatar answered Oct 09 '22 10:10

MatteoLacki