[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Problem with File.exists? giving an incorrect result

Chris Dwan

7/22/2007 10:21:00 AM

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.

<sigh> 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 <sob><sob>

Hope someone can enlighten me.
Thanks!


4 Answers

David and Sharon Phillips

7/22/2007 10:41:00 AM

0

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

> 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.
>
> <sigh> 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 <sob><sob>
>
> Hope someone can enlighten me.
> Thanks!

I assume you have permission to read the file?

John Joyce

7/22/2007 2:47:00 PM

0


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

> 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.
>
> <sigh> 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 <sob><sob>
>
> 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

Alex LeDonne

7/23/2007 1:12:00 AM

0

On 7/22/07, Chris Dwan <radixhound@gmail.com> wrote:
> 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.
>
> <sigh> 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 <sob><sob>
>
> 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

Nobuyoshi Nakada

7/23/2007 1:56:00 AM

0

Hi,

At Sun, 22 Jul 2007 19:21:19 +0900,
Chris Dwan wrote in [ruby-talk:261241]:
> <sigh> 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

What are the actual exception you get, and the version? And
$debug_log shows "YES - ..."?

--
Nobu Nakada