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

Saad Javed sbjaved at gmail.com
Mon Jun 30 16:17:45 CEST 2008


Here's the working code for my problem. But i tried it to post 'No
files found' in case no specified files are found. It doesn't do that.
Just simply exits.

dir_input = raw_input('Enter dir: ')

win_trace = ['*.ini', '*.db']

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':
				os.remove(filename)
				print 'done'
			elif confirmation == 'n':
				pass
			else:
				sys.exit()
		
On Sun, Jun 29, 2008 at 9:13 PM, Saad Javed <sbjaved at gmail.com> wrote:
> I transfer files a lot between my windows and linux partitions...these
> folders sometimes contain *.db and *.ini files which are not recognized or
> used by linux. So i tried to write a program to crawl through my home dir
> and remove these files...I'm *very* new to programming and python so please
> be gentle. Here is the code:
>
> import os
>
> list = ['*.ini', '*.db']
>
> for root, dirs, files in os.walk('/home/saad'):
> for list in files:
> os.remove(os.path.join('root', 'list'))
> print 'done'
>
> Unfortunately its a bit too efficient and nearly wiped my home dir before i
> manually killed it. Again...treat me like a super noob.
>
> Saad
>


More information about the Tutor mailing list