469,647 Members | 1,814 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,647 developers. It's quick & easy.

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='..\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
Mar 11 '08 #1
3 3372
On Mar 11, 10:35 am, royG <roygeor...@gmail.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
Mar 11 '08 #2
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
Mar 11 '08 #3
On Mar 11, 3:37 pm, Paul
Have a look at shutil.rmtree
thanks Paul
RG

Mar 11 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Daniel | last post: by
4 posts views Thread by Alex Kouzemtchenko | last post: by
117 posts views Thread by Peter Olcott | last post: by
28 posts views Thread by Jon Davis | last post: by
6 posts views Thread by Ammar | last post: by
reply views Thread by CSN | last post: by
2 posts views Thread by Mike Collins | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.