How to check the file is available or not

How to check the file is available or not.
write this simple code for checking the file.
but if file not found they print the first condition.

if(contents = File.read("c:/test/test.txt"))
              log.debug "Text File Available.....!!!!!!!!"
        else
              log.debug "File not Available"
              break
        end

···

--
Posted via http://www.ruby-forum.com/.

if test ?e, "c:/test/test.txt"

Jesus.

···

On Fri, May 14, 2010 at 7:57 AM, KingMaker KingMaker <sweetzubair@gmail.com> wrote:

How to check the file is available or not.
write this simple code for checking the file.
but if file not found they print the first condition.

if(contents = File.read("c:/test/test.txt"))
log.debug "Text File Available.....!!!!!!!!"
else
log.debug "File not Available"
break
end

KingMaker KingMaker wrote:

How to check the file is available or not.

You can do this with File.exist?("..."), but I'd do this:

begin
  contents = File.read("c:/test/test.txt")
  log.debug "Text File Available.....!!!!!!!!"
rescue Errno::ENOENT
  log.debug "File not available"
  break
end

This is technically better because it avoids a race condition (the file
being deleted within the short time between when you test for its
existence, and when you actually read it)

···

--
Posted via http://www.ruby-forum.com/\.

KingMaker KingMaker wrote:

How to check the file is available or not.

There is File.readable?(name)
but who knows if it actually works in windows...if you're not on windows
it probably works fine.

-rp

···

--
Posted via http://www.ruby-forum.com/\.