如何在ruby中递归创建目录? [英] How to create directories recursively in ruby?

查看:64
本文介绍了如何在ruby中递归创建目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将文件存储为/a/b/c/d.txt,但我不知道这些目录是否存在,需要时需要递归创建它们.如何在 ruby​​ 中做到这一点?

I want to store a file as /a/b/c/d.txt, but I do not know if any of these directories exist and need to recursively create them if necessary. How can one do this in ruby?

推荐答案

使用 mkdir_p:

FileUtils.mkdir_p '/a/b/c'

_p 是父/路径的 unix 保留,您也可以使用别名 mkpath 如果这对您更有意义.

The _p is a unix holdover for parent/path you can also use the alias mkpath if that makes more sense for you.

FileUtils.mkpath '/a/b/c'

在 Ruby 1.9 中,FileUtils 已从核心中删除,因此您必须require 'fileutils'.

In Ruby 1.9 FileUtils was removed from the core, so you'll have to require 'fileutils'.

这篇关于如何在ruby中递归创建目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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