Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby中的目录遍历_Ruby - Fatal编程技术网

Ruby中的目录遍历

Ruby中的目录遍历,ruby,Ruby,我一直在尝试使用简单的递归方法在Ruby中实现一个更大程序的目录遍历。但是,我发现它不包括其中的目录。我怎样才能把它们列出来 代码: 问题是,每次递归时,传递到File.directory?的路径都是no,只是实体(文件或目录)名称;所有上下文都丢失了。因此,假设您进入one/two/three/检查one/two/three/file.txt是否是一个目录,file.directory?只是从顶级目录的角度将“file.txt”作为路径,而不是整个路径。每次递归时都必须维护相对路径。这似乎很管

我一直在尝试使用简单的递归方法在Ruby中实现一个更大程序的目录遍历。但是,我发现它不包括其中的目录。我怎样才能把它们列出来

代码:


问题是,每次递归时,传递到
File.directory?
的路径都是no,只是实体(文件或目录)名称;所有上下文都丢失了。因此,假设您进入
one/two/three/
检查
one/two/three/file.txt
是否是一个目录,
file.directory?
只是从顶级目录的角度将
“file.txt”
作为路径,而不是整个路径。每次递归时都必须维护相对路径。这似乎很管用:

def walk(start)
  Dir.foreach(start) do |x|
    path = File.join(start, x)
    if x == "." or x == ".."
      next
    elsif File.directory?(path)
      puts path + "/" # remove this line if you want; just prints directories
      walk(path)
    else
      puts x
    end
  end
end

对于递归,应使用:

从文件中:

Find模块支持自顶向下遍历一组文件路径

例如,要合计主目录下所有文件的大小,请忽略“点”目录(例如$home/.ssh)中的任何内容:

需要“查找” 总尺寸=0 Find.Find(ENV[“HOME”])do | path| if FileTest.directory?(路径) 如果File.basename(路径)[0]==?。 Find.prune#不要进一步查看此目录。 其他的 下一个 结束 其他的 总大小+=FileTest.size(路径) 结束 结束
您可以使用
Dir.glob(start)
代替
Dir.foreach
。或者只使用
Dir[start]
。是否跳过
对吗?并希望在每次调用
walk(start)
?@iAmRubuuu Yes时遍历
start
中的其他人,以避免无限递归。@squiguy添加了一个答案,希望对您有所帮助。:)是的,它根本不打印任何东西。我已经玩了一段时间了。它是否在每个目录中?我觉得我做错了什么。我不怀疑你!假设我的桌面上有一个目录,其中有一些文件。我认为这个调用可以做到的是,如果我从我的桌面启动并点击该目录,它将重做漫游并输出该目录中的文件。非常好!嗯……我想知道为什么Ruby不把它当作目录名,而必须在directory方法中检查整个路径?因为路径是相对于当前工作目录的。这是一个操作系统,而不是Ruby。查看更多信息。基本上,如果您给
File.directory?
参数
“File.txt”
,它将只搜索当前工作目录(通常是程序所在的目录,除非您更改它)。因此,如果您想查看
somedir/file.txt
是否是一个目录,并且只给它
file.txt
,它将只在当前工作目录中搜索,而不在
somedir
中搜索该文件。很好的发现。我从来不知道这件事。
def walk(start)
  Dir.foreach(start) do |x|
    path = File.join(start, x)
    if x == "." or x == ".."
      next
    elsif File.directory?(path)
      puts path + "/" # remove this line if you want; just prints directories
      walk(path)
    else
      puts x
    end
  end
end
require 'find' total_size = 0 Find.find(ENV["HOME"]) do |path| if FileTest.directory?(path) if File.basename(path)[0] == ?. Find.prune # Don't look any further into this directory. else next end else total_size += FileTest.size(path) end end