os.removedirs not working

  • Thread starter Golawala, Moiz M (GE Infrastructure)
  • Start date
G

Golawala, Moiz M (GE Infrastructure)

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 (e-mail address removed)
www.gesecurity.com

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

Christopher T King

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!
 
C

CptPicard

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top