`
uglyduck^swam
  • 浏览: 6328 次
  • 性别: Icon_minigender_1
  • 来自: 南京
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

ruby application:关于文件的遍历

阅读更多
#这个是深度优先搜索
def find(*paths)
paths.collect!{|d| d.dup}
while file=paths.shift
catch(:prune) do
yield file.dup.taint
next unless File.exist? file
begin
if File.lstat(file).directory? then
d=Dir.open(file)
begin
for f in d
next if f=="." or f==".."
if File:: ALT_SEPARATOR and file=~/^(?:[\/\\]|[A-Za-z]:[\/\\]?)$/ then
f=file+f
elsif file=="/" then
f="/"+f
else
f=File.join(file,f)
end
paths.unshift f.untaint #要变成广度优先,最小限度的修改paths.unshift改为paths.push
end
ensure
d.close
end
end
rescue Errno::ENOENT,Errno::EACCES
end
end
end
end

find('/home/season/桌面/linux'){|f| puts f}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics