[Tutor] How do I delete all Files of certain extension type?

Mike Hoy mhoy4 at cox.net
Wed Jun 20 18:26:43 CEST 2007

I have a phonebook program that creates .pb files for each entry that is 
created. One of the features of the program will be to allow the user to 
delete all of the .pb files in the directory. For simplicity I was just 
going to have it delete all the .pb files in the same directory I was 
working in.

I've tried to make a while loop that will delete all the .pb file found 
from glob but it obviously is wrong:

Here is what I have so far:

def deleteAll():
    print "Here is a list of all Phonebook files in this directory: "
    pb_files = glob.glob('*.pb')
    print pb_files

    deleteFiles = raw_input("Would you like to delete all .pb files? ")
    if deleteFiles == "Yes":
    # need to loop thru pb_files and use os.remove
        while pb_files > 0:
    elif deleteFiles == "No":
        print "GoodBye"


Any pointers you can offer would be appreciated.


Mike Hoy

More information about the Tutor mailing list