Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a file exists

Tags:

ruby

So my problem is basically is that I can't figure out a condition to give an error message if can't find the file given as argument.

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

What I exactly need is the condition for the elseif.

like image 870
M. Adam Avatar asked Oct 23 '25 14:10

M. Adam


1 Answers

Try File.exist?, e.g.

2.3.0 :003 > File.exist? 'foo'
 => false 
like image 162
Andy Gaskell Avatar answered Oct 27 '25 01:10

Andy Gaskell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!