From: Tim Hunter Date: 2002-12-04T10:28:50+09:00 Subject: Re: Creating directories with File.open On Wed, 04 Dec 2002 09:20:53 +0900, Jason Persampieri wrote: > Ah... I see that in the 'Standard Library' section > now. Thanks. > > Out of curiosity... why wouldn't something like this > be in by default? Not to mention, why wouldn't 'open' > just do this automatically if File::CREAT is set? > > Jason > > --- Yukihiro Matsumoto wrote: >> Hi, >> >> In message "Creating directories with File.open" >> on 02/12/04, Jason Persampieri >> writes: >> >> |I'm attempting to create a file within a directory >> |structure that may or may not exist yet. >> | >> |File.open("tempdir/foo.txt","w") fails seemingly >> |because the 'tempdir' directory does not yet exist. >> >> |Is this proper behavior? If the user wants to >> create >> |the FILE, why would File#open assume they wouldn't >> |want to create the underlying directory as well? >> | >> |Is there another method that WOULD go ahead and >> create >> |the directory for me? >> >> Put >> >> require 'ftools' >> File.mkpath "tempdir" >> >> or >> >> require 'fileutils' >> FileUtils.mkpath "tempdir" >> >> before "open". >> >> matz. >> Mostly, I think, because that's just not the way it has traditionally been done in UNIX-ish O/S's. For example, in the C library, the fopen/open functions don't make non-existent directories for you. You must create the directory first and then create the file. For another example, the mkdir command _will_ create non-existing parent directories, but not by default. You have to specify the -p option.