Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > rmdir problem

Reply
Thread Tools

rmdir problem

 
 
royG
Guest
Posts: n/a
 
      03-11-2008
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
 
Reply With Quote
 
 
 
 
Paul Hankin
Guest
Posts: n/a
 
      03-11-2008
On Mar 11, 10:35 am, royG <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Tim Golden
Guest
Posts: n/a
 
      03-11-2008
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
 
Reply With Quote
 
royG
Guest
Posts: n/a
 
      03-11-2008
On Mar 11, 3:37 pm, Paul
> Have a look at shutil.rmtree
>

thanks Paul
RG

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[HELP W2k] need folder deleted with RMDIR Sean Computer Support 4 06-02-2007 06:13 AM
Trouble with FileUtils.rm_rf / .rmdir James B. Byrne Ruby 0 02-21-2006 06:11 PM
rmdir Question George Kinley Perl Misc 11 05-23-2004 07:52 PM
Problem problem problem :( Need Help Mike ASP General 2 05-11-2004 08:36 AM



Advertisments