Digest Articles 261154-261275 (11/13) (ruby-talk ML)

From: John Joyce <dangerwillrobinsondanger@gmail.com>
Date: July 22, 2007 7:46:39 AM PDT (CA)

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 window

That doesn't make any sense! File.exists? is a class method, and if I try to do File.open, it will fail because the file doesn't exist, so I'll have to catch the exception. It basically means File.exists? is useless so I should just use exceptions to handle stuff like this.. ?

···

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

You can't read certain files that exist; like directories, for
example. I'm not sure what Windows XP does for a guest user trying to
read hidden or system files.

Todd

···

On 7/22/07, Chris Dwan <radixhound@gmail.com> wrote:

That doesn't make any sense! File.exists? is a class method, and if I
try to do File.open, it will fail because the file doesn't exist, so
I'll have to catch the exception. It basically means File.exists? is
useless so I should just use exceptions to handle stuff like this.. ?

You might check the file type, and file permissions.
try File.readable? or other File methods to check out the file.
I suggest using File.open because you then can control your flow more carefully. You maybe reading a file that is too big to manage all at once by using File.read
But you say the file is renamed by the same script?
Then whenever and wherever that renaming occurs, you need to update your ibl and subsequent iblLocation variables.
Pay close attention to the variable scope. consider using @ibl instead to keep it accessible. Otherwise you may not actually be getting the current location.

···

On Jul 22, 2007, at 5:58 PM, Chris Dwan wrote:

From: John Joyce <dangerwillrobinsondanger@gmail.com>
Date: July 22, 2007 7:46:39 AM PDT (CA)

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 window

That doesn't make any sense! File.exists? is a class method, and if I try to do File.open, it will fail because the file doesn't exist, so I'll have to catch the exception. It basically means File.exists? is useless so I should just use exceptions to handle stuff like this.. ?