ruby 检查文件名是文件夹还是文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10115591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:00:05  来源:igfitidea点击:

Check if a filename is a folder or a file

rubyfile

提问by icn

I have a little piece of Ruby code:

我有一小段 Ruby 代码:

files.each do |file|
  FileUtils.mkdir_p(File.dirname(target))    
  FileUtils.cp_r(file, target, :verbose => true)
end

I would like to add a check like

我想添加一个支票

if file is a folder
  # do this
if file is a file
  # do that

How do I implement in Ruby?

我如何在 Ruby 中实现?

回答by thelazydeveloper

You can use File.directory?("name")and/or File.file?("name").

您可以使用File.directory?("name")和/或File.file?("name").

回答by mbigras

Also a good idea to check out Pathname#directory?and Pathname#file?

也是一个不错的主意,检查出Pathname#directory?Pathname#file?