Winston Pan
Winston Pan

Reputation: 47

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 *?

Upvotes: 0

Views: 965

Answers (2)

DeepSpace
DeepSpace

Reputation: 81594

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.

Upvotes: 2

Adon Bilivit
Adon Bilivit

Reputation: 26976

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)

Upvotes: 1

Related Questions