Delete multiple files matching a pattern

Using the glob module:

import glob, os
for f in glob.glob("P*.jpg"):
    os.remove(f)

Alternatively, using pathlib:

from pathlib import Path
for p in Path(".").glob("P*.jpg"):
    p.unlink()

Try something like this:

import os, re

def purge(dir, pattern):
    for f in os.listdir(dir):
        if re.search(pattern, f):
            os.remove(os.path.join(dir, f))

Then you would pass the directory containing the files and the pattern you wish to match.


If you need recursion into several subdirectories, you can use this method:

import os, re, os.path
pattern = "^(?P<photo_id>\d+)[^\d].*jpg$"
mypath = "Photos"
for root, dirs, files in os.walk(mypath):
    for file in filter(lambda x: re.match(pattern, x), files):
        os.remove(os.path.join(root, file))

You can safely remove subdirectories on the fly from dirs, which contains the list of the subdirectories to visit at each node.

Note that if you are in a directory, you can also get files corresponding to a simple pattern expression with glob.glob(pattern). In this case you would have to substract the set of files to keep from the whole set, so the code above is more efficient.


How about this?

import glob, os, multiprocessing
p = multiprocessing.Pool(4)
p.map(os.remove, glob.glob("P*.jpg"))

Mind you this does not do recursion and uses wildcards (not regex).

UPDATE In Python 3 the map() function will return an iterator, not a list. This is useful since you will probably want to do some kind processing on the items anyway, and an iterator will always be more memory-efficient to that end.

If however, a list is what you really need, just do this:

...
list(p.map(os.remove, glob.glob("P*.jpg")))

I agree it's not the most functional way, but it's concise and does the job.