[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to create a file

Zhao Yi

1/13/2009 7:10:00 AM

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.
--
Posted via http://www.ruby-....

3 Answers

Robert Klemme

1/13/2009 7:16:00 AM

0

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.ht...

Cheers

robert

--
remember.guy do |as, often| as.you_can - without end

Dave Bass

1/15/2009 11:17:00 AM

0

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
--
Posted via http://www.ruby-....

Zhao Yi

1/16/2009 12:41:00 AM

0

Robert Klemme 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.ht...
>
> Cheers
>
> robert

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

thanks.
--
Posted via http://www.ruby-....