对指定多个目录的第一级保留进行保留(再递归删除空目录)

对指定多个目录的第一级保留进行保留(再递归删除空目录):

注:
1、先从1个文件里读取多个大目录
2、再将大目录里的第一级目录抓取后存到另1文件
3、再对第一级目录的子目录判断是否为空

vi a1.py

#!/usr/bin/python

import os

os.remove('/root/python/logs/py.log')
os.mknod('/root/python/logs/py.log')

g = open('/root/python/py.txt','r')
for h in g.readlines():
g.close()
e = h.strip()
os.chdir(e)
for o in os.listdir(e):
if os.path.isdir(os.path.join(e,o)):
m = open('/root/python/logs/py.log','a')
m.write(os.path.join(e,o)+"\n")
m.close()

n = open('/root/python/logs/py.log','r')
for p in n.readlines():
n.close()
q = p.strip()
os.chdir(q)
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

猜你喜欢

转载自blog.51cto.com/yangzhiming/2128919
今日推荐