# Problem with File.exists? giving an incorrect result

**URL:** <https://www.ruby-forum.com/t/problem-with-file-exists-giving-an-incorrect-result/110064>\
**Category:** Ruby\
**Created:** [July 22, 2007, 12:22pm UTC](https://www.ruby-forum.com/t/problem-with-file-exists-giving-an-incorrect-result/110064 "2007-07-22T12:22:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [July 22, 2007, 12:22pm UTC](https://www.ruby-forum.com/t/problem-with-file-exists-giving-an-incorrect-result/110064/1 "2007-07-22T12:22:16Z")

</div>

I have a problem with a script I’m working on. File.exists? is  
telling me that a file does in fact exist, then immediately after  
when I try to do a File.read, it crashes because the file doesn’t  
exist. I know that it does not exist, because I’m testing that  
condition. If the file doesn’t exist, it should do something else.

this is on Windows, so maybe the OS is to blame?  
Here’s the code, if it helps…

```
         $debug_log.fatal("does file exist? :: #{ibl}")
         if File.exist?(ibl)
           $debug_log.fatal("YES - it most certainlyt does!")
           iblLocation = ibl
         else
             #find the file and set iblLocation
         end
         File.read(iblLocation) # << bombs here

```

The file referred to by ibl was recently renamed by this same script,  
so the file name in ibl is no longer valid. The script should then go  
looking for ibl’s new name and use that… but it won’t work

Hope someone can enlighten me.  
Thanks!

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [July 22, 2007, 12:41pm UTC](https://www.ruby-forum.com/t/problem-with-file-exists-giving-an-incorrect-result/110064/2 "2007-07-22T12:41:56Z")

</div>

On 22/07/2007, at 8:21 PM, Chris D. wrote:

> ```
> if File.exist?(ibl)
> 
> ```
> 
> won’t work
> 
> Hope someone can enlighten me.  
> Thanks!

I assume you have permission to read the file?

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [July 22, 2007, 4:48pm UTC](https://www.ruby-forum.com/t/problem-with-file-exists-giving-an-incorrect-result/110064/3 "2007-07-22T16:48:08Z")

</div>

On Jul 22, 2007, at 5:21 AM, Chris D. wrote:

> ```
> if File.exist?(ibl)
> 
> ```
> 
> won’t work
> 
> Hope someone can enlighten me.  
> Thanks!

You need to open the file first.  
File.new OR File.open  
check them with ri first, you may need to choose binary or text when  
setting the file mode because you’re using windows

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [July 23, 2007, 3:57am UTC](https://www.ruby-forum.com/t/problem-with-file-exists-giving-an-incorrect-result/110064/4 "2007-07-23T03:57:14Z")

</div>

Hi,

At Sun, 22 Jul 2007 19:21:19 +0900,  
Chris D. wrote in [ruby-talk:261241]:

> ```
> File.read(iblLocation) # << bombs here
> 
> ```

What are the actual exception you get, and the version? And  
$debug\_log shows “YES - …”?

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [July 23, 2007, 3:12am UTC](https://www.ruby-forum.com/t/problem-with-file-exists-giving-an-incorrect-result/110064/5 "2007-07-23T03:12:40Z")

</div>

On 7/22/07, Chris D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> if File.exist?(ibl)
> 
> ```
> 
> Hope someone can enlighten me.  
> Thanks!

What does the rename code look like? I’ve seen lots of weirdness on  
Windows because file operations that you might think are atomic,  
aren’t.

-A
