只能删除空文件夹,删除非空文件夹会报错

>>> import os
>>> os.rmdir("/tmp/ssh-GyoPWOFZ47")
Traceback (most recent call last):
File "<stdin>", line , in <module>
OSError: [Errno ] Directory not empty: '/tmp/ssh-GyoPWOFZ47' >>> os.removedirs("/tmp/ssh-GyoPWOFZ47")
Traceback (most recent call last):
File "<stdin>", line , in <module>
File "/usr/lib/python2.7/os.py", line , in removedirs
rmdir(name)
OSError: [Errno ] Directory not empty: '/tmp/ssh-GyoPWOFZ47'

经测试下面的方法,可以删除非空文件夹,(#空目录、有内容的目录都可以删)

>>> import shutil
>>> shutil.rmtree("/tmp/ssh-GyoPWOFZ47")
>>>
>>> exit()

这里只删除了 ssh-GyoPWOFZ47 文件夹

05-11 13:56