Alan Gauld wrote:
"David" <[EMAIL PROTECTED]> wrote
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'
Note that at this point you have printed both names but you have not
stored a reference to them.
Thus filename contains only the last name.
You need to create a list of the valid filenames.
You could use a list compreghension like so:
files = [f for f in glob.glob(win_trace_path) if os.path.exists(f)]
print files
confirmation = raw_input('Confirm removal: ')
if confirmation == 'y':
print "removing '%s'" % filename
os.remove(filename)
files_removed += 1
And now you are removing only one file, but you need to remove all of
the files in your list so add a loop like:
if confirmation == 'y':
for filename in files:
print "removing '%s'" % filename
os.remove(filename)
files_removed += 1
elif confirmation == 'n':
pass
else:
sys.exit()
The elif doesn't do anything so you can delete it.
HTH,
Thanks Alan,
I am learning python in a group and one of the members came 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', '*.txt']
files_removed = 0
file_list = []
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
file_list.append(filename)
else:
print 'No files found'
confirmation = raw_input('Confirm removal: ')
if confirmation == 'y':
for file in file_list:
print "removing " + file
os.remove(file)
files_removed += 1
if files_removed :
print '%d files removed' % files_removed
else :
print 'No files removed'
Can see why it did not work before. That is progress.
--
Powered by Gentoo GNU/LINUX
http://www.linuxcrazy.com
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor