recursively removing files and directories

Discussion in 'Python' started by bahoo, Apr 9, 2007.

  1. bahoo

    bahoo Guest

    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
     
    bahoo, Apr 9, 2007
    #1
    1. Advertising

  2. On Apr 9, 1:44 pm, "bahoo" <> wrote:
    > 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
     
    Eli Criffield, Apr 9, 2007
    #2
    1. Advertising

  3. 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
     
    Eli Criffield, Apr 9, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken
    Replies:
    1
    Views:
    3,768
    Roedy Green
    Dec 2, 2005
  2. Trinity
    Replies:
    1
    Views:
    527
    Peter van Merkerk
    Nov 7, 2003
  3. rbt
    Replies:
    5
    Views:
    9,017
  4. albert kao
    Replies:
    5
    Views:
    725
    Walter Wefft
    May 16, 2010
  5. Alain.Feler
    Replies:
    3
    Views:
    128
    Ghislain Mary
    May 15, 2005
Loading...

Share This Page