recursively removing files and directories

B

bahoo

Hi,

I found a message on Jan 16, 2006 regarding the same topic, except
that I wanted to remove only certain files that satisfy the format
"ABC_XXX_XXX.dat", but not the other files. Once the files are
removed, if a folder becomes empty, I want to remove the folder as
well.

The solution to the Jan 16 2006 message required many lines of python
code. I was wondering if there is a simpler solution to my problem at
hand, perhaps by using more specialized functions?

Thanks!
bahoo
 
E

Eli Criffield

Hi,

I found a message on Jan 16, 2006 regarding the same topic, except
that I wanted to remove only certain files that satisfy the format
"ABC_XXX_XXX.dat", but not the other files. Once the files are
removed, if a folder becomes empty, I want to remove the folder as
well.

The solution to the Jan 16 2006 message required many lines of python
code. I was wondering if there is a simpler solution to my problem at
hand, perhaps by using more specialized functions?

Thanks!
bahoo

Something like

import os
import re

def processFiles(args,dir,fileList):
for thisFile in fileList:
if re.match(r'REGEXPATTERN',thisFile):
os.unlink("%s%s"dir,thisFile)

os.path.walk("/",processFiles,None)

But thats just off the top of my head, so that mite not be exact.

Eli Criffield
 
E

Eli Criffield

Forgot the rmdir

import os
import re

def processFiles(args,dir,fileList):
for thisFile in fileList:
if re.match(r'REGEXPATTERN',thisFile):
os.unlink("%s%s"dir,thisFile)
os.rmdir(dir)

os.path.walk("/",processFiles,None)



Eli Criffield
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top