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

Leave a Comment