# Test if file exists

**URL:** <https://www.ruby-forum.com/t/test-if-file-exists/94803>\
**Category:** Ruby\
**Created:** [May 15, 2006, 10:06am UTC](https://www.ruby-forum.com/t/test-if-file-exists/94803 "2006-05-15T10:06:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pieter-schutte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/da6949/32.png) [@pieter-schutte](https://www.ruby-forum.com/u/pieter-schutte)\
**Post date:** [May 15, 2006, 10:06am UTC](https://www.ruby-forum.com/t/test-if-file-exists/94803/1 "2006-05-15T10:06:47Z")

</div>

Hi,

Look at this :

if (ARGV.size != 2)  
puts ‘usage: ./compare file1 file2’

else  
filename1 = ARGV[0]  
filename2 = ARGV[1]

* * *

How would I test if the arguments given (filename1 and 2) are actually  
files  
that exist?

Thanks.

---

<div class="post-metadata">

**Author:** ![pieter-schutte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/da6949/32.png) [@pieter-schutte](https://www.ruby-forum.com/u/pieter-schutte)\
**Post date:** [May 15, 2006, 10:12am UTC](https://www.ruby-forum.com/t/test-if-file-exists/94803/2 "2006-05-15T10:12:54Z")

</div>

On 5/15/06, Pieter S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,

Hi,

[…]

> How would I test if the arguments given (filename1 and 2) are actually files  
> that exist?

File.exists?(filename1)

or

require ‘pathname’  
Pathname.new(filename1).exists?

---

<div class="post-metadata">

**Author:** ![pieter-schutte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/da6949/32.png) [@pieter-schutte](https://www.ruby-forum.com/u/pieter-schutte)\
**Post date:** [May 15, 2006, 10:15am UTC](https://www.ruby-forum.com/t/test-if-file-exists/94803/3 "2006-05-15T10:15:59Z")

</div>

File.exists?(file\_name)

---

<div class="post-metadata">

**Author:** ![pieter-schutte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/da6949/32.png) [@pieter-schutte](https://www.ruby-forum.com/u/pieter-schutte)\
**Post date:** [May 15, 2006, 10:22am UTC](https://www.ruby-forum.com/t/test-if-file-exists/94803/4 "2006-05-15T10:22:17Z")

</div>

Thanks alot!

That’s so simple, hehe!
