2017-06-11 204 views
0

所以我的問題基本上是,我無法弄清楚的條件給出錯誤信息,如果找不到指定的參數文件。如何檢查文件是否存在

if ARGV.empty? 
    puts "Give me a file!" 
elseif [condition] 
    puts "Can't find the file" 
else 
    file = File.open(ARGV[0]) 

我確切需要的是elseif的條件。

+0

Ruby的'File.open'如果找不到文件就會拋出異常。這不夠好嗎? –

+0

的可能的複製[如何檢查文件的存在(https://stackoverflow.com/questions/8590098/how-to-check-for-file-existence) –

回答

2

嘗試File.exist?,例如

2.3.0 :003 > File.exist? 'foo' 
=> false