寻找 。 - 在 ruby​​ 中输入 f

发布于 2024-08-16 22:39:12 字数 200 浏览 2 评论 0原文

我想获取特定目录下所有文件的列表。 Dir.glob 效果很好,但似乎没有办法将结果限制为仅文件(不包括目录)。

这就是我现在所拥有的:

files = Dir.glob('my_dir/**/*').reject { |f| File.directory?(f) }

是否有更优雅的方法来完成此任务?

I want to grab a list of all the files under a particular directory. Dir.glob works great, but there doesn't seem to be a way to limit results to just files (excluding directories).

Heres's what I have right now:

files = Dir.glob('my_dir/**/*').reject { |f| File.directory?(f) }

Is there a more elegant way to accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栖竹 2024-08-23 22:39:12

这实际上是一种相当有效的方法,但您也可以使用 Find 模块:

require 'find'

found = [ ]

Find.find(base_path) do |path|
  found << path if (File.file?(path))
end

That's actually a fairly efficient way to go about doing it, but you might also use the Find module:

require 'find'

found = [ ]

Find.find(base_path) do |path|
  found << path if (File.file?(path))
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文