# Unable to write to file... (example from "pickaxe book", second edition, p128)

**URL:** <https://rubytalk.org/t/unable-to-write-to-file-example-from-pickaxe-book-second-edition-p128/24172>\
**Category:** ruby-talk\
**Created:** [19 January 2006 10:49 UTC](https://rubytalk.org/t/unable-to-write-to-file-example-from-pickaxe-book-second-edition-p128/24172 "2006-01-19T10:49:43Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Maclean](https://avatars.discourse-cdn.com/v4/letter/j/e8c25b/32.png) [@John\_Maclean](https://rubytalk.org/u/John_Maclean)\
**Post date:** [19 January 2006 10:49 UTC](https://rubytalk.org/t/unable-to-write-to-file-example-from-pickaxe-book-second-edition-p128/24172/1 "2006-01-19T10:49:43Z")

</div>

Following the example from "pickaxe book", second edition, p128

#!/usr/bin/ruby  
File.open("testfile", "r") do |file|  
&nbsp;&nbsp;while line = file.gets  
&nbsp;&nbsp;&nbsp;&nbsp;puts line  
&nbsp;&nbsp;end  
end

produces  
copy.rb:2:in `initialize': No such file or directory - testfile (Errno::ENOENT)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from copy.rb:2

> **···**
>
> --  
> John Maclean  
> MSc (DIC)  
> 07739 171 531

---

<div class="post-metadata">

**Author:** ![HT\_de\_Beer1](https://avatars.discourse-cdn.com/v4/letter/h/ea666f/32.png) [@HT\_de\_Beer1](https://rubytalk.org/u/HT_de_Beer1)\
**Post date:** [19 January 2006 10:54 UTC](https://rubytalk.org/t/unable-to-write-to-file-example-from-pickaxe-book-second-edition-p128/24172/2 "2006-01-19T10:54:39Z")

</div>

The problem is clear. You are trying to read a file which does not exist. So,  
to solve the `problem', you create a file named testfile, put some text in  
it, and there you go,

> **···**
>
> On Thursday 19 January 2006 11:49, John Maclean wrote:
> 
> > Following the example from "pickaxe book", second edition, p128
> > 
> > #!/usr/bin/ruby  
> > File.open("testfile", "r") do |file|  
> > &nbsp;&nbsp;while line = file.gets  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts line  
> > &nbsp;&nbsp;end  
> > end
> > 
> > produces  
> > copy.rb:2:in `initialize': No such file or directory - testfile  
> > (Errno::ENOENT) from copy.rb:2
> 
> --  
> HT de Beer

---

<div class="post-metadata">

**Author:** ![Zach\_Dennis1](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@Zach\_Dennis1](https://rubytalk.org/u/Zach_Dennis1)\
**Post date:** [19 January 2006 10:56 UTC](https://rubytalk.org/t/unable-to-write-to-file-example-from-pickaxe-book-second-edition-p128/24172/3 "2006-01-19T10:56:55Z")

</div>

John Maclean wrote:

> Following the example from "pickaxe book", second edition, p128
> 
> #!/usr/bin/ruby File.open("testfile", "r") do |file|  
> &nbsp;&nbsp;while line = file.gets  
> &nbsp;&nbsp;&nbsp;&nbsp;puts line  
> &nbsp;&nbsp;end  
> end
> 
> produces  
> copy.rb:2:in `initialize': No such file or directory - testfile (Errno::ENOENT)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from copy.rb:2

John, you need the file named "testfile" to exist before you can run that code. It can't open a file if it doesn't exist!

Zach

---

<div class="post-metadata">

**Author:** ![John\_Maclean](https://avatars.discourse-cdn.com/v4/letter/j/e8c25b/32.png) [@John\_Maclean](https://rubytalk.org/u/John_Maclean)\
**Post date:** [19 January 2006 11:20 UTC](https://rubytalk.org/t/unable-to-write-to-file-example-from-pickaxe-book-second-edition-p128/24172/4 "2006-01-19T11:20:37Z")

</div>

Thanks! The light shines. So how can i -write- to a file using the code below as an example?

> **···**
>
> On Thu, 19 Jan 2006 19:56:55 +0900 zdennis \<zdennis@mktec.com\> wrote:
> 
> > John Maclean wrote:  
> > \> Following the example from "pickaxe book", second edition, p128  
> > \>  
> > \> #!/usr/bin/ruby  
> > \> File.open("testfile", "r") do |file|  
> > \> while line = file.gets  
> > \> puts line  
> > \> end  
> > \> end  
> > \>  
> > \> produces  
> > \> copy.rb:2:in `initialize': No such file or directory - testfile (Errno::ENOENT)  
> > \> from copy.rb:2  
> > \>
> > 
> > John, you need the file named "testfile" to exist before you can run that code. It can't open a file  
> > if it doesn't exist!
> > 
> > Zach
> 
> --  
> John Maclean  
> MSc (DIC)  
> 07739 171 531

---

<div class="post-metadata">

**Author:** ![Diego\_Algorta\_Casama](https://avatars.discourse-cdn.com/v4/letter/d/53a042/32.png) [@Diego\_Algorta\_Casama](https://rubytalk.org/u/Diego_Algorta_Casama)\
**Post date:** [19 January 2006 11:35 UTC](https://rubytalk.org/t/unable-to-write-to-file-example-from-pickaxe-book-second-edition-p128/24172/5 "2006-01-19T11:35:54Z")

</div>

John Maclean escribió:

> Thanks! The light shines. So how can i -write- to a file using the code below as an example?

Change  
\>\>\> File.open("testfile", "r") do |file|  
to  
\>\>\> File.open("testfile", "w") do |file|

Read:  
[http://www.ruby-doc.org/core/classes/File.html](http://www.ruby-doc.org/core/classes/File.html)  
[http://www.ruby-doc.org/core/classes/IO.html](http://www.ruby-doc.org/core/classes/IO.html)

Diego

> **···**
>
> > On Thu, 19 Jan 2006 19:56:55 +0900 \> zdennis \<zdennis@mktec.com\> wrote:
> > 
> > > John Maclean wrote:
> > > 
> > > > Following the example from "pickaxe book", second edition, p128
> > > > 
> > > > #!/usr/bin/ruby File.open("testfile", "r") do |file|  
> > > > &nbsp;&nbsp;while line = file.gets  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;puts line  
> > > > &nbsp;&nbsp;end  
> > > > end
> > > > 
> > > > produces  
> > > > copy.rb:2:in `initialize': No such file or directory - testfile (Errno::ENOENT)  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from copy.rb:2
> > > 
> > > John, you need the file named "testfile" to exist before you can run that code. It can't open a file if it doesn't exist!
> > > 
> > > Zach

---

<div class="post-metadata">

**Author:** ![Jacob\_Fugal](https://avatars.discourse-cdn.com/v4/letter/j/439d5e/32.png) [@Jacob\_Fugal](https://rubytalk.org/u/Jacob_Fugal)\
**Post date:** [19 January 2006 16:10 UTC](https://rubytalk.org/t/unable-to-write-to-file-example-from-pickaxe-book-second-edition-p128/24172/6 "2006-01-19T16:10:02Z")

</div>

Well you need more than that. For one thing the file.gets will fail if  
file was opened in write mode. Also the puts is going to STDOUT, not  
the file. You probably want something like this:

#!/usr/bin/ruby  
File.open("testfile", "w") do |file|  
&nbsp;&nbsp;while line = gets  
&nbsp;&nbsp;&nbsp;&nbsp;file.puts line  
&nbsp;&nbsp;end  
end

This will get input from STDIN and write it out to test file. Program  
terminates on EOF marker (CTRL-D when run interactively).

Jacob Fugal

> **···**
>
> On 1/19/06, Diego Algorta Casamayou \<ruby@dac.e4ward.com\> wrote:
> 
> > Change  
> > \>\>\> File.open("testfile", "r") do |file|  
> > to  
> > \>\>\> File.open("testfile", "w") do |file|

---

<div class="post-metadata">

**Author:** ![Mike\_Fletcher](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@Mike\_Fletcher](https://rubytalk.org/u/Mike_Fletcher)\
**Post date:** [19 January 2006 16:29 UTC](https://rubytalk.org/t/unable-to-write-to-file-example-from-pickaxe-book-second-edition-p128/24172/7 "2006-01-19T16:29:13Z")

</div>

John Maclean wrote:

> Thanks! The light shines. So how can i -write- to a file using the code  
> below as an example?

Open a file for writing

File.open( "infile", "r" ) | in |  
&nbsp;&nbsp;File.open( "outfile", "w" ) | out |  
&nbsp;&nbsp;&nbsp;&nbsp;while line = in.gets  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.print line  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

See the docs for IO which explain all the mode flags ("r", "w", "a",  
etc.).

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Diego\_Algorta\_Casama](https://avatars.discourse-cdn.com/v4/letter/d/53a042/32.png) [@Diego\_Algorta\_Casama](https://rubytalk.org/u/Diego_Algorta_Casama)\
**Post date:** [19 January 2006 16:38 UTC](https://rubytalk.org/t/unable-to-write-to-file-example-from-pickaxe-book-second-edition-p128/24172/8 "2006-01-19T16:38:10Z")

</div>

Jacob Fugal escribió:

> **···**
>
> > On 1/19/06, Diego Algorta Casamayou \<ruby@dac.e4ward.com\> wrote:
> > 
> > > Change  
> > > \>\>\> File.open("testfile", "r") do |file|  
> > > to  
> > > \>\>\> File.open("testfile", "w") do |file|
> > 
> > Well you need more than that. For one thing the file.gets will fail if  
> > file was opened in write mode. Also the puts is going to STDOUT, not  
> > the file. You probably want something like this:
> > 
> > #!/usr/bin/ruby  
> > File.open("testfile", "w") do |file|  
> > &nbsp;&nbsp;while line = gets  
> > &nbsp;&nbsp;&nbsp;&nbsp;file.puts line  
> > &nbsp;&nbsp;end  
> > end
> > 
> > This will get input from STDIN and write it out to test file. Program  
> > terminates on EOF marker (CTRL-D when run interactively).
> 
> That's right. I focused my reply on on the open method, but missed the full example.
> 
> Diego

---

<div class="post-metadata">

**Author:** ![Guest2](https://avatars.discourse-cdn.com/v4/letter/g/439d5e/32.png) [@Guest2](https://rubytalk.org/u/Guest2)\
**Post date:** [3 November 2006 17:24 UTC](https://rubytalk.org/t/unable-to-write-to-file-example-from-pickaxe-book-second-edition-p128/24172/9 "2006-11-03T17:24:16Z")

</div>

Mike Fletcher wrote:

> John Maclean wrote:
> 
> > Thanks! The light shines. So how can i -write- to a file using the code  
> > below as an example?
> 
> Open a file for writing
> 
> File.open( "infile", "r" ) | in |  
> &nbsp;&nbsp;File.open( "outfile", "w" ) | out |  
> &nbsp;&nbsp;&nbsp;&nbsp;while line = in.gets  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.print line  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> end
> 
> See the docs for IO which explain all the mode flags ("r", "w", "a",  
> etc.).

Do you think this script work?

Li

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Marcel\_Ward](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@Marcel\_Ward](https://rubytalk.org/u/Marcel_Ward)\
**Post date:** [3 November 2006 18:08 UTC](https://rubytalk.org/t/unable-to-write-to-file-example-from-pickaxe-book-second-edition-p128/24172/10 "2006-11-03T18:08:21Z")

</div>

It fails because "in" is a reserved word in Ruby and cannot be used as  
a variable name. Try changing the two instances of "in" to "inp" and  
it will work.

> **···**
>
> On 03/11/06, Li Chen \<chen\_li3@yahoo.com\> wrote:
> 
> > Mike Fletcher wrote:  
> > \> John Maclean wrote:  
> > \> [snip]  
> > \>  
> > \> File.open( "infile", "r" ) | in |  
> > \> File.open( "outfile", "w" ) | out |  
> > \> while line = in.gets  
> > \> out.print line  
> > \> end  
> > \> end  
> > \> end
> > 
> > Do you think this script work?
> 
> --  
> Marcel

---

<div class="post-metadata">

**Author:** ![Mike\_Fletcher](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@Mike\_Fletcher](https://rubytalk.org/u/Mike_Fletcher)\
**Post date:** [3 November 2006 19:34 UTC](https://rubytalk.org/t/unable-to-write-to-file-example-from-pickaxe-book-second-edition-p128/24172/11 "2006-11-03T19:34:06Z")

</div>

Li Chen wrote:

> Mike Fletcher wrote:
> 
> > John Maclean wrote:
> > 
> > > Thanks! The light shines. So how can i -write- to a file using the code  
> > > below as an example?
> > 
> > Open a file for writing
> > 
> > File.open( "infile", "r" ) | in |  
> > &nbsp;&nbsp;File.open( "outfile", "w" ) | out |  
> > &nbsp;&nbsp;&nbsp;&nbsp;while line = in.gets  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.print line  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end  
> > end
> > 
> > See the docs for IO which explain all the mode flags ("r", "w", "a",  
> > etc.).
> 
> Do you think this script work?

As is no (I blame lack of caffeine and/or sleep); but with "do" inserted  
in the right two places and the reserved word "in" replaced with  
something that's not a reserved word ( say "inf" ) it works just fine.

One should take all example code posted in haste to mailing lists with a  
grain of salt.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
