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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了递归删除指定路径下的空目录相关的知识,希望对你有一定的参考价值。

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

注:
如第一级目录要保留

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)+" ")
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

以上是关于递归删除指定路径下的空目录的主要内容,如果未能解决你的问题,请参考以下文章