'in shutil.rmtree() how to match the string?

In linux bash we can delete files through rm -rf test_file*, but in Python shutil.rmtree(). How can we match the string through some thing like *?



Solution 1:[1]

None of the functions in the shutil module expand the path. It's possible to use the glob module and then call shutil.rmtree on each result:

import shutil

from glob import glob

for match in glob('test_file*'):
   shutil.rmtree(match)

Alternatively you can call directly rm using the subprocess module.

Solution 2:[2]

Here's an example of how you could do this:

import os
from glob import glob

for file in glob('test_file*'):
    os.remove(file)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DeepSpace
Solution 2 Albert Winestein