如何检查文件是否存在

mus*_*tze 8 ruby path pathname

我有一个字符串数组,我想只选择这些作为文件路径的字符串:

我的路径是"~/dlds/some_file.ics"其中~/dlds的一个符号链接到~/archive/downloads我的系统上.该文件具有以下权限:

-rw-r--r--
Run Code Online (Sandbox Code Playgroud)

我的代码(我尝试了几种变体):

ARGV.select do |string|
    File.file? string # returns false
    Pathname.new(string).file? # returns false
    Pathname.new(string).expand_path.file? # returns false
end
Run Code Online (Sandbox Code Playgroud)

我不知道还有什么可以尝试的.

我正在运行Ruby 2.2.0或2.2.2.

Ale*_*kin 15

File.exist? File.expand_path "~/dlds/some_file.ics"
Run Code Online (Sandbox Code Playgroud)

  • 请注意,如果路径也是目录,则返回true.详情请访问:http://stackoverflow.com/questions/8590098/ruby-function-to-check-file-existence (3认同)