By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,006 Members | 1,111 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,006 IT Pros & Developers. It's quick & easy.

os.removedirs not working

P: n/a
Hi All,

I have a small program that where I am using os.removedirs('C:\\someDir') on windows. I get the error that the directory is not empty. Sure there are sub-directories under it and all file and directories are deletable(nothing is locked by the system) and there are no permission issues either. Can someone please tell me if I am using the os.removedirs() correctly?

Moiz Golawala
GE Infrastructure, Security
Software Engineer
Enterprise Solutions

T 561 994 5972
F 561 994 6572
E mo***********@ge.com
www.gesecurity.com

791 Park of Commerce Blvd., Suite 100
Boca Raton, FL, 33487, U.S.A.
GE Security, Inc.

Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Thu, 12 Aug 2004, Golawala, Moiz M (GE Infrastructure) wrote:
I have a small program that where I am using
os.removedirs('C:\\someDir') on windows. I get the error that the
directory is not empty. Sure there are sub-directories under it and all
file and directories are deletable(nothing is locked by the system) and
there are no permission issues either. Can someone please tell me if I
am using the os.removedirs() correctly?


os.removedirs() won't remove files for you. As far as I can tell, there's
no builtin Python function that does this, but the library docs for os
give the following bit of code that does just what you want:

import os
from os.path import join
# Delete everything reachable from the directory named in 'top'.
# CAUTION: This is dangerous! For example, if top == '/', it
# could delete all your disk files.
for root, dirs, files in os.walk(top, topdown=False):
for name in files:
os.remove(join(root, name))
for name in dirs:
os.rmdir(join(root, name))

My guess as to why this isn't a library function is precisely the reason
stated in that comment: it's potentially dangerous!

Jul 18 '05 #2

P: n/a
You could use shutil.rmtree : it works fine!
But I tend to prefer the solution offered by Chistopher T King: It gives you
more control on what you are doing.

"Christopher T King" <sq******@WPI.EDU> wrote in message
news:Pi*************************************@ccc8. wpi.edu...
On Thu, 12 Aug 2004, Golawala, Moiz M (GE Infrastructure) wrote:
I have a small program that where I am using
os.removedirs('C:\\someDir') on windows. I get the error that the
directory is not empty. Sure there are sub-directories under it and all
file and directories are deletable(nothing is locked by the system) and
there are no permission issues either. Can someone please tell me if I
am using the os.removedirs() correctly?


os.removedirs() won't remove files for you. As far as I can tell, there's
no builtin Python function that does this, but the library docs for os
give the following bit of code that does just what you want:

import os
from os.path import join
# Delete everything reachable from the directory named in 'top'.
# CAUTION: This is dangerous! For example, if top == '/', it
# could delete all your disk files.
for root, dirs, files in os.walk(top, topdown=False):
for name in files:
os.remove(join(root, name))
for name in dirs:
os.rmdir(join(root, name))

My guess as to why this isn't a library function is precisely the reason
stated in that comment: it's potentially dangerous!

Jul 18 '05 #3

P: n/a
On Thu, 12 Aug 2004, CptPicard wrote:
You could use shutil.rmtree : it works fine!


Oh wow, I completely forgot about shutil. Good stuff, that.

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.