I know how to delete single files, however I am lost in my implementation of how to delete all files in a directory of one type.
Say the directory is \\myfolder
Here ya go:
import os
# Return all files in dir, and all its subdirectories, ending in pattern
def gen_files(dir, pattern):
for dirname, subdirs, files in os.walk(dir):
for f in files:
if f.endswith(pattern):
yield os.path.join(dirname, f)
# Remove all files in the current dir matching *.config
for f in gen_files('.', '.config'):
os.remove(f)
Note also that gen_files
can be easily rewritten to accept a tuple of patterns, since str.endswith
accepts a tuple