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

Kent Johnson kent37 at tds.net
Wed Jun 20 20:20:55 CEST 2007


Mike Hoy 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)       

This is not the right way to iterate over a list. Try this:
   for pb_file in pb_files:
     os.remove(pb_file)

Kent

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



More information about the Tutor mailing list