# How to create a file

**URL:** <https://www.ruby-forum.com/t/how-to-create-a-file/157566>\
**Category:** Ruby\
**Created:** [January 13, 2009, 8:10am UTC](https://www.ruby-forum.com/t/how-to-create-a-file/157566 "2009-01-13T08:10:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [January 13, 2009, 8:10am UTC](https://www.ruby-forum.com/t/how-to-create-a-file/157566/1 "2009-01-13T08:10:26Z")

</div>

I use this code to write to a file: File.new(@FILE\_NAME,“w”). If the  
file name doesn’t exist, I will get “No such file” error. How can I  
create the file if it doesn’t exist?

thanks.

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [January 13, 2009, 8:20am UTC](https://www.ruby-forum.com/t/how-to-create-a-file/157566/2 "2009-01-13T08:20:57Z")

</div>

On 13.01.2009 08:09, Zhao Yi wrote:

> I use this code to write to a file: File.new(@FILE\_NAME,“w”). If the  
> file name doesn’t exist, I will get “No such file” error. How can I  
> create the file if it doesn’t exist?

I cannot reproduce this behavior. Did you make sure that the directory  
exists where you want to create your file? In case not you probably  
want to have a look at FileUtils.mkdir\_p:

[http://ruby-doc.org/stdlib/libdoc/fileutils/rdoc/classes/FileUtils.html#M000676](http://ruby-doc.org/stdlib/libdoc/fileutils/rdoc/classes/FileUtils.html#M000676)

Cheers

robert

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [January 15, 2009, 12:17pm UTC](https://www.ruby-forum.com/t/how-to-create-a-file/157566/3 "2009-01-15T12:17:33Z")

</div>

It could be that the operating system is unable to create a file with  
that file name. For instance on Windows:

C:\>irb  
irb(main):001:0\> @FILE\_NAME = “.”  
=\> “.”  
irb(main):002:0\> File.new(@FILE\_NAME, “w”)  
Errno::EACCES: Permission denied - .  
from (irb):2:in `initialize' from (irb):2:in`new’  
from (irb):2

This could be a particular problem when using international character  
sets.

Dave

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [January 16, 2009, 1:42am UTC](https://www.ruby-forum.com/t/how-to-create-a-file/157566/4 "2009-01-16T01:42:04Z")

</div>

Robert K. wrote:

> On 13.01.2009 08:09, Zhao Yi wrote:
> 
> > I use this code to write to a file: File.new(@FILE\_NAME,“w”). If the  
> > file name doesn’t exist, I will get “No such file” error. How can I  
> > create the file if it doesn’t exist?
> 
> I cannot reproduce this behavior. Did you make sure that the directory  
> exists where you want to create your file? In case not you probably  
> want to have a look at FileUtils.mkdir\_p:
> 
> [http://ruby-doc.org/stdlib/libdoc/fileutils/rdoc/classes/FileUtils.html#M000676](http://ruby-doc.org/stdlib/libdoc/fileutils/rdoc/classes/FileUtils.html#M000676)
> 
> Cheers
> 
> robert

The directory doesn’t exist, that’s the problem.

thanks.
