# File.new error

**URL:** <https://www.ruby-forum.com/t/file-new-error/89378>\
**Category:** Ruby\
**Created:** [January 4, 2006, 2:02pm UTC](https://www.ruby-forum.com/t/file-new-error/89378 "2006-01-04T14:02:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cow6oy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ebca7d/32.png) [@cow6oy](https://www.ruby-forum.com/u/cow6oy)\
**Post date:** [January 4, 2006, 2:02pm UTC](https://www.ruby-forum.com/t/file-new-error/89378/1 "2006-01-04T14:02:45Z")

</div>

Hi Forum,

I’m trying to create a file but I’m getting an error and can’t find the  
problem:

* * *

aFile = File.new(“rubytestfile”)

```
puts "hello"

```

## aFile.close

I get this error. I’m working on Mac OS X:

Errno::ENOENT: No such file or directory - rubytestfile

thanks for any help!

---

<div class="post-metadata">

**Author:** ![cow6oy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ebca7d/32.png) [@cow6oy](https://www.ruby-forum.com/u/cow6oy)\
**Post date:** [January 4, 2006, 2:16pm UTC](https://www.ruby-forum.com/t/file-new-error/89378/2 "2006-01-04T14:16:13Z")

</div>

Kai V. wrote:

> ## aFile.close
> 
> I get this error. I’m working on Mac OS X:
> 
> Errno::ENOENT: No such file or directory - rubytestfile
> 
> thanks for any help!

Try

aFile = File.new(“rubytestfile”, “w”)

If you don’t specify the 2nd argument (the open mode) Ruby assumes you  
want to open the file for reading only. If the file doesn’t exist, you  
can’t read it.

---

<div class="post-metadata">

**Author:** ![cow6oy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ebca7d/32.png) [@cow6oy](https://www.ruby-forum.com/u/cow6oy)\
**Post date:** [January 4, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/file-new-error/89378/3 "2006-01-04T14:19:41Z")

</div>

Timothy H. wrote:

> > 
> 
> Try
> 
> aFile = File.new(“rubytestfile”, “w”)
> 
> If you don’t specify the 2nd argument (the open mode) Ruby assumes you  
> want to open the file for reading only. If the file doesn’t exist, you  
> can’t read it.

And please use the block form from the start, i.e.

File.open(“rubytestfile”, “w”) do |aFile|  
aFile.puts “hello”  
end

Btw, the original script wrote to stdout and not to the file.

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![cow6oy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ebca7d/32.png) [@cow6oy](https://www.ruby-forum.com/u/cow6oy)\
**Post date:** [February 2, 2006, 2:35pm UTC](https://www.ruby-forum.com/t/file-new-error/89378/4 "2006-02-02T14:35:37Z")

</div>

Could this method be extended to acept input from stdin? Something  
similar to:-

def testwrite  
File.open(“testfile”, “w”) { |file| file.gets “test” }  
end  
testwrite

The intention is to read lines from stdnin and put them into the file…

On Wed, 4 Jan 2006 22:03:03 +0900 Kai V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![cow6oy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ebca7d/32.png) [@cow6oy](https://www.ruby-forum.com/u/cow6oy)\
**Post date:** [February 5, 2006, 4:47pm UTC](https://www.ruby-forum.com/t/file-new-error/89378/5 "2006-02-05T16:47:25Z")

</div>

Hi,

In message “Re: File.new error”  
on Thu, 2 Feb 2006 22:33:35 +0900, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:

|Could this method be extended to acept input from stdin? Something  
|similar to:-  
|  
|def testwrite  
| File.open(“testfile”, “w”) { |file| file.gets “test” }  
|end  
|testwrite  
|  
|The intention is to read lines from stdnin and put them into the file…

require ‘fileutils’

def testwrite  
File.open(“testfile”, “w”) {|file|  
FileUtils.copy\_stream(STDIN, file)  
}  
end
