rmdir problem

Discussion in 'Python' started by royG, Mar 11, 2008.

  1. royG

    royG Guest

    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='..\mytestdir'
    #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
    royG, Mar 11, 2008
    #1
    1. Advertising

  2. royG

    Paul Hankin Guest

    On Mar 11, 10:35 am, royG <> wrote:
    > 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
    Paul Hankin, Mar 11, 2008
    #2
    1. Advertising

  3. royG

    Tim Golden Guest

    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='..\mytestdir'
    > #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:

    http://docs.python.org/lib/module-shutil.html


    TJG
    Tim Golden, Mar 11, 2008
    #3
  4. royG

    royG Guest

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

    thanks Paul
    RG
    royG, Mar 11, 2008
    #4
    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. Andreas Suurkuusk
    Replies:
    0
    Views:
    3,968
    Andreas Suurkuusk
    Jul 27, 2003
  2. Ted Miller
    Replies:
    0
    Views:
    5,145
    Ted Miller
    Sep 13, 2003
  3. Mike

    Problem problem problem :( Need Help

    Mike, May 7, 2004, in forum: ASP General
    Replies:
    2
    Views:
    543
    Bullschmidt
    May 11, 2004
  4. James B. Byrne

    Trouble with FileUtils.rm_rf / .rmdir

    James B. Byrne, Feb 21, 2006, in forum: Ruby
    Replies:
    0
    Views:
    201
    James B. Byrne
    Feb 21, 2006
  5. George Kinley

    rmdir Question

    George Kinley, May 19, 2004, in forum: Perl Misc
    Replies:
    11
    Views:
    167
    Eric Bohlman
    May 23, 2004
Loading...

Share This Page