递归删除空目录

先用Python做的,对中文支持实在搞不懂,英文目录没问题。

# -*- coding:utf-8 -*-

import os

def RecursiveDelEmptyDir(path):
	files = os.listdir(path)
	for f in files:
		if os.path.isdir(os.path.join(path,f)):
			RecursiveDelEmptyDir(os.path.join(path,f))
	if not files:
		os.rmdir(path)
	
if __name__ == '__main__':
	RecursiveDelEmptyDir(r'g:\media')

换Ruby做,非常流畅,这方面个人觉得Ruby胜出!

#encoding:gbk

require 'fileutils'

def RecursiveDelEmptyDir(path)
	Dir.entries(path).each do |sub|
		unless sub == '..'
			unless sub == '.'
				if File.directory?(File.join(path,sub))
					RecursiveDelEmptyDir File.join(path,sub)
				end				
			end			
		end		
	end
	if Dir.entries(path).length == 2
				FileUtils.rmdir path
	end
end

RecursiveDelEmptyDir 'g:\\media'


你可能感兴趣的:(递归删除空目录)