I am trying to figure out how to open up a text file in a subdirectory of my programs main directory. The file I want is in the subdirectory labeled text. I get a "Permission denied - text/" message when attempting to run the code found below.
campershash = Hash.new
Dir.entries("text").each do |textfilename|
campershash["#{textfilename.chop.chop.chop.chop}"] = File.open("text/" + "#{textfilename}") { |f| f.gets(nil) }
end
The Dir.entries call works finer but on the next line when I go to open up each text file in the text directory I run into problems. I am running the program on windows XP with administrator privileges so I am unclear as to why I am having permission issues.
Any and all help is greatly appreciated,
Matthew Margolis
You're probably trying to open a directory (. and .. in 'text/'). Try
one of the two examples below:
campershash = {}
Dir.entries("text").each do |textfilename|
# Don't know why you're using four chop's - but I've replaced them
# with 'strip' to make the example more readable.
campershash[textfilename.strip] =
File.read(File.join("text", textfilename)) if File.file?(textfilename)
end
or
campershash = {}
Dir["text/*"].each do |f|
campershash[f.strip] = File.read(f)
end
//Anders
···
On Thu, Aug 05, 2004 at 09:46:53AM +0900, Matthew Margolis wrote:
I am trying to figure out how to open up a text file in a subdirectory
of my programs main directory. The file I want is in the subdirectory
labeled text. I get a "Permission denied - text/" message when
attempting to run the code found below.
campershash = Hash.new
Dir.entries("text").each do |textfilename|
campershash["#{textfilename.chop.chop.chop.chop}"] =
File.open("text/" + "#{textfilename}") { |f| f.gets(nil) }
end
The Dir.entries call works finer but on the next line when I go to open
up each text file in the text directory I run into problems. I am
running the program on windows XP with administrator privileges so I am
unclear as to why I am having permission issues.
--
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. Anders Engström aengstrom@gnejs.net
. http://www.gnejs.net PGP-Key: ED010E7F
. [Your mind is like an umbrella. It doesn't work unless you open it.]
"Matthew Margolis" <mrmargolis@wisc.edu> schrieb im Newsbeitrag
news:41119195.7010502@wisc.edu...
I am trying to figure out how to open up a text file in a subdirectory
of my programs main directory. The file I want is in the subdirectory
labeled text. I get a "Permission denied - text/" message when
attempting to run the code found below.
campershash = Hash.new
Dir.entries("text").each do |textfilename|
campershash["#{textfilename.chop.chop.chop.chop}"] =
File.open("text/" + "#{textfilename}") { |f| f.gets(nil) }
end
campershash = {}
Dir.entries("text").each do |textfilename|
key = textfilename[0...-4] and
campershash[key] = File.read( File.join("text", textfilename) )
end