File creation

All
This is embarrassing:
How do I create a file?

# construct the log file name. It does not exist as yet.
lfil = leg_dir+log_name
puts "Log file name is: #{lfil}"
# Create the file
if !File.exists?(lfil)
    puts "we entered file exixts condition"
  begin
    fOut = File.new(lfil, "w+")
    fOut.puts("Thomas the Tank")
  rescue SystemCallError
    puts "failed to create new file"
  end
end
fOut.close unless fOut.nil?

I keep getting
jusplay.rb:15:in `initialize': No such file or directory -
D:/REINHARD/DEV/LOG/hello.log (Errno::ENOENT)
        from jusplay.rb:15:in `new'
        from jusplay.rb:15

Thanks

···

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

try "File.open" in stat of File.new (ka if it different)

check if File.writable?(File.dirname(lfil))

···

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

You need to make sure that the directory that will contain the file
(D:/REINHARD/DEV/LOG/) exists before creating the file. You can either
do that outside your script before you run it or from within your script:

require 'fileutils'
FileUtils.mkdir_p(File.dirname(lfil))

-Jeremy

···

On 10/14/2010 01:12 AM, Reinhard Lange wrote:

All
This is embarrassing:
How do I create a file?

# construct the log file name. It does not exist as yet.
lfil = leg_dir+log_name
puts "Log file name is: #{lfil}"
# Create the file
if !File.exists?(lfil)
    puts "we entered file exixts condition"
  begin
    fOut = File.new(lfil, "w+")
    fOut.puts("Thomas the Tank")
  rescue SystemCallError
    puts "failed to create new file"
  end
end
fOut.close unless fOut.nil?

I keep getting
jusplay.rb:15:in `initialize': No such file or directory -
D:/REINHARD/DEV/LOG/hello.log (Errno::ENOENT)
        from jusplay.rb:15:in `new'
        from jusplay.rb:15