rmdir problem

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

    rmdir problem

    hi
    i am checking if a directory exists and if it does i want to delete it
    and its contents.then i want to create the directory before creating
    files in it.

    def myfolderops():
    testdir='..\myt estdir'
    #if dir exist remove it
    if isdir(testdir):
    rmdir(testdir)
    #again create directory
    mkdir(testdir)

    I am working on WinXP and logged in as admin in WinXP. when there is
    no dir called '..\mytestdir' or an empty dir this code works removing
    and creating the directory.but if the directory exists with contents
    already then it causes an error 145 when rmdir is executed.the message
    says 'directory is not empty'
    what should i do to correct this?
    (i need to remove the dir with its contents because each time i will
    be putting diff files into it and donot want them to be mixed with old
    files)

    thanks
    RG
  • Paul Hankin

    #2
    Re: rmdir problem

    On Mar 11, 10:35 am, royG <roygeor...@gma il.comwrote:
    i am checking if a directory exists and if it does i want to delete it
    and its contents.then i want to create the directory before creating
    files in it.
    Have a look at shutil.rmtree

    --
    Paul Hankin

    Comment

    • Tim Golden

      #3
      Re: rmdir problem

      royG wrote:
      hi
      i am checking if a directory exists and if it does i want to delete it
      and its contents.then i want to create the directory before creating
      files in it.
      >
      def myfolderops():
      testdir='..\myt estdir'
      #if dir exist remove it
      if isdir(testdir):
      rmdir(testdir)
      #again create directory
      mkdir(testdir)
      >
      I am working on WinXP and logged in as admin in WinXP. when there is
      no dir called '..\mytestdir' or an empty dir this code works removing
      and creating the directory.but if the directory exists with contents
      already then it causes an error 145 when rmdir is executed.the message
      says 'directory is not empty'
      what should i do to correct this?
      (i need to remove the dir with its contents because each time i will
      be putting diff files into it and donot want them to be mixed with old
      files)
      Two things:

      1) Use raw strings (r"..\blah") or forward slashes ("../blah") when
      messing with path names under windows.

      2) Check out the shutils module:

      Source code: Lib/shutil.py The shutil module offers a number of high-level operations on files and collections of files. In particular, functions are provided which support file copying and removal...



      TJG

      Comment

      • royG

        #4
        Re: rmdir problem

        On Mar 11, 3:37 pm, Paul
        Have a look at shutil.rmtree
        >
        thanks Paul
        RG

        Comment

        Working...