How remove directories with the content in the platform independent way?

Andy Leszczynski leszczynscy at nospamyahoo.com
Tue Apr 26 22:50:00 EDT 2005


Andy Leszczynski wrote:
> James Stroud wrote:
> 
>> Look at the os and os.path modules.
>>
>> http://docs.python.org/lib/module-os.html
>> http://docs.python.org/lib/module-os.path.html
>>
>>
>>
>> On Tuesday 26 April 2005 07:27 pm, so sayeth Andy Leszczynski:
>>
>>> How remove directories with the content in the platform independent way?
>>> Is the API for that?
>>>
>>> Thx, A.
>>
>>
>>
> It is not clear if rmdir would remove the non-empty dir ...
> 
never min, I found this in that doc :-)

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




More information about the Python-list mailing list