[Tutor] Deleting specified files using a python program...help with code?]

Alan Gauld alan.gauld at btinternet.com
Tue Jul 1 10:47:59 CEST 2008

"David" <david at abbottdavid.com> wrote 

Thats better.

Now on a wee usability aspect:

> confirmation = raw_input('Confirm removal: ')

You might want to print the list of files before asking the 
question. Although if its a likely to be a long list maybe just
say how many files - it gives the user a chance to back out 
if it sounds suspicious!

Also, its good to tell the user what are valid values to type
or to provide a default value:

confirmation = raw_input('Confirm removal[y/n]: ')


confirmation = raw_input('Confirm removal[yes]: ')

> if confirmation == 'y':

And to cover slight errors of input, like 'Y' or 'yes' try this:

if confirmation[0] in 'yY':

Which will correctly process any input beginning 
with y or Y which is more friendly.

Some prefer a slightly different route to the same end:

if confirmation.lower().startswith('y')

But I find the in technique more flexible.

If you go with the default approach the test becomes:

if confirmation in 'yY' or not confirmation:
      # go ahead and delete them


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list