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

David david at abbottdavid.com
Mon Jun 30 22:54:33 CEST 2008


I am learning python from books and this mailing list. I used the 
suggestions to come up with this;
#!/usr/bin/python
# Filename : new_remove-file.py
import os
import sys
import glob

dir_input = raw_input('Enter dir: ')
win_trace = ['*.ini', '*.db']
files_removed = 0
for root, dirs, files in os.walk(dir_input):
    for trace in win_trace:
        win_trace_path = os.path.join(root, trace)
        for filename in glob.glob(win_trace_path):
            if os.path.exists(filename):
                print filename
            else:
                print 'No files found'
confirmation = raw_input('Confirm removal: ')
if confirmation == 'y':
    print "removing '%s'" % filename
    os.remove(filename)
    files_removed += 1
elif confirmation == 'n':
    pass
else:
    sys.exit()
if files_removed :
    print '%d files removed' % files_removed
else :
    print 'No files found'
Here is the output;

david at opteron ~ $ ./new_remove_file.py
Enter dir: test
test/test.ini
test/test.db
Confirm removal: y
removing 'test/test.db'
1 files removed

Please point me in the direction as to why only one file is removed.
thanks
-david


-- 
Powered by Gentoo GNU/LINUX
http://www.linuxcrazy.com



More information about the Tutor mailing list