Python 的 os.walk 的 Ruby 等价物是什么? [英] What's the Ruby equivalent of Python's os.walk?

查看:47
本文介绍了Python 的 os.walk 的 Ruby 等价物是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道 Ruby 中是否有现有的模块/函数来遍历文件系统目录和文件?我正在寻找类似于 Python 的 os.walk 的东西.我找到的最接近的模块是 Find 但需要一些额外的工作来进行遍历.

Does anyone know if there's an existing module/function inside Ruby to traverse file system directories and files? I'm looking for something similar to Python's os.walk. The closest module I've found is Find but requires some extra work to do the traversal.

Python 代码如下所示:

The Python code looks like the following:

for root, dirs, files in os.walk('.'):
    for name in files:
        print name
    for name in dirs:
        print name

推荐答案

以下将递归打印所有文件.那你可以用File.directory吗?看看它是目录还是文件.

The following will print all files recursively. Then you can use File.directory? to see if the it is a directory or a file.

Dir['**/*'].each { |f| print f }

这篇关于Python 的 os.walk 的 Ruby 等价物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆