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

F.Lee aristotle831 at gmail.com
Wed Jun 20 20:45:17 CEST 2007

On 6/20/07, Mike Hoy <mhoy4 at cox.net> wrote:
> 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:
>             os.remove(pb_files)
>     elif deleteFiles == "No":
>         print "GoodBye"
> deleteAll()
> Any pointers you can offer would be appreciated.
> Thanks,
> Mike Hoy
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor


A couple of things:
1. pb_files is a list. You never change the list, yet you say "while
2. os.remove takes a path as its parm, not a list.

suggestion: How 'bout something like
for f in pb_files:

In fact, since you're removing all of 'em, you could just do
os.system("rm *.pb") ## assumes *nix OS



More information about the Tutor mailing list