<div>Here is some code that could be useful.... It does not check the directories for 0 files, but that shouldn't be too difficult to add.</div>
<div> </div>
<div>#############################################</div>
<div>from os import listdir, unlink<br>from os.path import isdir, isfile, islink, join, getmtime<br>deldir = "C:\Some\Dir"<br>delFileType = ".txt" # or You could use Regex.<br># -------------------------------------
<br>def del_entry(_name):<br> try:<br> if isdir(_name):<br> print "Do Nothing"<br> else:<br> #print "now"<br> unlink(_name) # or remove(_name)
<br> sys.stdout.write("Delete FILE %s\n" % (_name))<br> except IOError:<br> sys.stderr.write("Cannot delete %s\n" % (_name))<br># -------------------------------------
<br>def list_dir(_dir,_action):<br> if not isdir(_dir):<br> print "%s is not a directory" % (_dir)<br> return<br> for file in listdir(_dir):<br> path = join(_dir, file)
<br> if isdir(path):<br> list_dir(path, _action)<br> else:<br> # isfile() or islink()<br> if path.rfind(delFileType) != -1:
<br> #print path<br> _action(path)<br># Run it<br>list_dir(deldir, del_entry)<br>
<div>#############################################</div><br> </div>
<div><span class="gmail_quote">On 9 Apr 2007 11:44:10 -0700, <b class="gmail_sendername">bahoo</b> <<a href="mailto:b83503104@yahoo.com">b83503104@yahoo.com</a>> wrote:</span>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">Hi,<br><br>I found a message on Jan 16, 2006 regarding the same topic, except<br>that I wanted to remove only certain files that satisfy the format
<br>"ABC_XXX_XXX.dat", but not the other files. Once the files are<br>removed, if a folder becomes empty, I want to remove the folder as<br>well.<br><br>The solution to the Jan 16 2006 message required many lines of python
<br>code. I was wondering if there is a simpler solution to my problem at<br>hand, perhaps by using more specialized functions?<br><br>Thanks!<br>bahoo<br><br>--<br><a href="http://mail.python.org/mailman/listinfo/python-list">
http://mail.python.org/mailman/listinfo/python-list</a><br></blockquote></div><br>