[Tutor] How do I delete all Files of certain extension type?
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:
> elif deleteFiles == "No":
> print "GoodBye"
> Any pointers you can offer would be appreciated.
> Mike Hoy
> Tutor maillist - Tutor at python.org
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