递归删除指定路径下的空目录

递归删除指定路径下的空目录:

注:
如第一级目录要保留

vi a1.py

#!/usr/bin/python

import os

dir="/root/python"

os.chdir(dir)

for f in os.listdir(dir):
if os.path.isdir(f):
p = open('/root/python/logs/py.log','a')
p.write(os.path.join(dir,f)+"\n")
p.close()

g = open('/root/python/logs/py.log','r')
for i in g.readlines():
g.close()
f = i.strip()
os.chdir(f)
for root,dirs,files in os.walk('.'):
for j in dirs:
if not os.listdir(os.path.join(root,j)) and os.path.join(root,j) != f:
print(os.path.join(root,j))
os.removedirs(os.path.join(root,j))

:wq

python a1.py

猜你喜欢

转载自blog.51cto.com/yangzhiming/2128830