# File.new raise an exception?

**URL:** https://rubytalk.org/t/file-new-raise-an-exception/26119
**Category:** ruby-talk
**Created:** [26 March 2006 15:51 UTC](https://rubytalk.org/t/file-new-raise-an-exception/26119 "2006-03-26T15:51:11Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Jean-Francois\_Boisv1](https://avatars.discourse-cdn.com/v4/letter/j/13edae/32.png) [@Jean-Francois\_Boisv1](https://rubytalk.org/u/Jean-Francois_Boisv1)
#### Post date: [26 March 2006 15:51 UTC](https://rubytalk.org/t/file-new-raise-an-exception/26119/1 "2006-03-26T15:51:11Z")

</div>

Hello,

I'm pretty new to ruby and I've written some code to parse apache logs, it works but now I want to store the result in a file. The problem is as follow (using irb)

irb(main):001:0\> foo = File.open("test")  
Errno::ENOENT: No such file or directory - test  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):1:in `initialize'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):1:in `open'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):1

I just do not understand the problem ( I can create a file with python in the same directory ), what's wrong for me ?

thanks for a quick answer

Jean-François

---

<div class="post-metadata">

### Author: ![Patrick\_Hof](https://avatars.discourse-cdn.com/v4/letter/p/b5ac83/32.png) [@Patrick\_Hof](https://rubytalk.org/u/Patrick_Hof)
#### Post date: [26 March 2006 16:38 UTC](https://rubytalk.org/t/file-new-raise-an-exception/26119/2 "2006-03-26T16:38:58Z")

</div>

Jean-François Boisvieux \<jfb@mxs.fr\> schrieb:

> irb(main):001:0\> foo = File.open("test")  
> Errno::ENOENT: No such file or directory - test  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):1:in `initialize'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):1:in `open'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):1
> 
> I just do not understand the problem ( I can create a file with  
> python in the same directory ), what's wrong for me ?

Ruby is opening the file for reading per default. As there seems to be  
no file with the name test, you get the error. To open it for writing,  
you have to say

File.open("test", "w")

Patrick

> **···**
>
> --  
> "Ah. Philosophy," said Om.  
> (Small Gods)

---

<div class="post-metadata">

### Author: ![Jean-Francois\_Boisv1](https://avatars.discourse-cdn.com/v4/letter/j/13edae/32.png) [@Jean-Francois\_Boisv1](https://rubytalk.org/u/Jean-Francois_Boisv1)
#### Post date: [26 March 2006 16:58 UTC](https://rubytalk.org/t/file-new-raise-an-exception/26119/3 "2006-03-26T16:58:11Z")

</div>

Thanks Patrick for this precise and clear answer

Jean-Francois

> **···**
>
> Le 26 mars 06 à 18:38, Patrick Hof a écrit :
> 
> > Jean-François Boisvieux \<jfb@mxs.fr\> schrieb:
> > 
> > > irb(main):001:0\> foo = File.open("test")  
> > > Errno::ENOENT: No such file or directory - test  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):1:in `initialize'  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):1:in `open'  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):1
> > > 
> > > I just do not understand the problem ( I can create a file with  
> > > python in the same directory ), what's wrong for me ?
> > 
> > Ruby is opening the file for reading per default. As there seems to be  
> > no file with the name test, you get the error. To open it for writing,  
> > you have to say
> > 
> > File.open("test", "w")
> > 
> > Patrick
> > 
> > --  
> > "Ah. Philosophy," said Om.  
> > (Small Gods)
