Deleting Directories

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Laura McCord

    Deleting Directories


    This is probably a very basic question but I started learning python. I
    am almost done writing my delete directory script but I am at a stand
    still right now.
    I want to delete folders in my "/var/www/html/da" directory that are
    over 1 day old.

    But, when I find the folder a simple rmdir() command does not work
    because the directory is not empty. What else do I need to do to delete
    a directory that contains content?

    Here is the code:

    dir = "/var/www/html/da"
    currentTime = int(time.time() )
    print currentTime
    dirfiles = os.listdir(dir)
    print dirfiles
    for name in dirfiles:
    dirpath = os.path.join(di r, name)
    mod_time = os.path.getmtim e(dirpath)
    timeDiff = currentTime - mod_time
    if timeDiff > maxOld:
    print dirpath

    Thanks,
    Laura

  • Rico Huijbers

    #2
    Re: Deleting Directories

    Laura McCord wrote:[color=blue]
    > This is probably a very basic question but I started learning python. I
    > am almost done writing my delete directory script but I am at a stand
    > still right now.
    > I want to delete folders in my "/var/www/html/da" directory that are
    > over 1 day old.
    >
    > But, when I find the folder a simple rmdir() command does not work
    > because the directory is not empty. What else do I need to do to delete
    > a directory that contains content?[/color]

    Assuming you already figured out how to check the directories'
    timestamps, shutil.rmtree() will do what you want.

    Comment

    • Peter Hansen

      #3
      Re: Deleting Directories

      Laura McCord wrote:
      [color=blue]
      > This is probably a very basic question but I started learning python. I
      > am almost done writing my delete directory script but I am at a stand
      > still right now.
      > I want to delete folders in my "/var/www/html/da" directory that are
      > over 1 day old.
      >
      > But, when I find the folder a simple rmdir() command does not work
      > because the directory is not empty. What else do I need to do to delete
      > a directory that contains content?[/color]

      There is an example at the bottom of
      http://docs.python.org/lib/os-file-dir.html which does what you need.

      I've also been able to get shutil.rmtree() to do it before, as I recall,
      with an appropriate error handler, though I vaguely recall it had to
      restart in some way after each directory which failed to be removed
      the first time because it was not empty.

      -Peter

      Comment

      • Peter Hansen

        #4
        Re: Deleting Directories

        Peter Hansen wrote:
        [color=blue][color=green]
        >> But, when I find the folder a simple rmdir() command does not work
        >> because the directory is not empty. What else do I need to do to delete
        >> a directory that contains content?[/color]
        >
        > I've also been able to get shutil.rmtree() to do it before, as I recall,
        > with an appropriate error handler, though I vaguely recall it had to
        > restart in some way after each directory which failed to be removed
        > the first time because it was not empty.[/color]

        Since Rico didn't give a disclaimer about the directories being
        empty, I just did a little test and sure enough, rmtree will
        wipe out everything without complaint. As it turns out, the
        only time you need special handling is if some of the files
        are not deletable.

        -Peter

        Comment

        • 336699

          #5
          Re: Deleting Directories

          Look at the docs for the shutil module. It has a function called
          rmtree() that will do what you want.

          "Laura McCord" <Laura.McCord@d oucet-austin.com> wrote in message news:<mailman.1 38.1085151361.6 949.python-list@python.org >...
          [color=blue]
          > I want to delete folders in my "/var/www/html/da" directory that are
          > over 1 day old.
          >
          > But, when I find the folder a simple rmdir() command does not work
          > because the directory is not empty. What else do I need to do to delete
          > a directory that contains content?[/color]

          Comment

          Working...