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:innew’
from jusplay.rb:15

Thanks

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

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

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

begin
D:/REINHARD/DEV/LOG/hello.log (Errno::ENOENT)
from jusplay.rb:15:in `new’
from jusplay.rb:15

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs