# File creation

**URL:** <https://www.ruby-forum.com/t/file-creation/197418>\
**Category:** Ruby\
**Created:** [October 14, 2010, 8:12am UTC](https://www.ruby-forum.com/t/file-creation/197418 "2010-10-14T08:12:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![okkezSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/85f322/32.png) [@okkezSS](https://www.ruby-forum.com/u/okkezSS)\
**Post date:** [October 14, 2010, 8:12am UTC](https://www.ruby-forum.com/t/file-creation/197418/1 "2010-10-14T08:12:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![raperswil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6a8cbe/32.png) [@raperswil](https://www.ruby-forum.com/u/raperswil)\
**Post date:** [October 14, 2010, 8:50am UTC](https://www.ruby-forum.com/t/file-creation/197418/2 "2010-10-14T08:50:09Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![raperswil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6a8cbe/32.png) [@raperswil](https://www.ruby-forum.com/u/raperswil)\
**Post date:** [October 14, 2010, 2:54pm UTC](https://www.ruby-forum.com/t/file-creation/197418/3 "2010-10-14T14:54:47Z")

</div>

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
