递归删除指定路径下的空目录:
注:
如第一级目录要保留
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)):
print(os.path.join(root,j))
os.removedirs(os.path.join(root,j))
:wq
python a1.py
原文地址:http://blog.51cto.com/yangzhiming/2128830
时间: 2024-10-09 01:06:17