How to create directories recursively in ruby?

后端 未结 6 1889
醉酒成梦
醉酒成梦 2020-12-02 13:54

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?

6条回答
  •  有刺的猬
    2020-12-02 14:23

    Use mkdir_p to create directory recursively

    path = "/tmp/a/b/c"
    
    FileUtils.mkdir_p(path) unless File.exists?(path)
    

提交回复
热议问题