File creation

R

Reinhard Lange

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
 
H

Hans Mackowiak

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

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

Jeremy Bopp

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top