open files in other directories

  • Thread starter Matthew Margolis
  • Start date
M

Matthew Margolis

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
 
A

Anders Engström

--cNdxnHkX5QqsyA0e
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

I am trying to figure out how to open up a text file in a subdirectory=20
of my programs main directory. The file I want is in the subdirectory=20
labeled text. I get a "Permission denied - text/" message when=20
attempting to run the code found below.
=20
campershash =3D Hash.new
Dir.entries("text").each do |textfilename|
campershash["#{textfilename.chop.chop.chop.chop}"] =3D=20
File.open("text/" + "#{textfilename}") { |f| f.gets(nil) }
end
=20
The Dir.entries call works finer but on the next line when I go to open= =20
up each text file in the text directory I run into problems. I am=20
running the program on windows XP with administrator privileges so I am= =20
unclear as to why I am having permission issues.
=20

You're probably trying to open a directory (. and .. in 'text/'). Try
one of the two examples below:

campershash =3D {}
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] =3D=20
File.read(File.join("text", textfilename)) if File.file?(textfilena=
me)
end

or

campershash =3D {}
Dir["text/*"].each do |f|
campershash[f.strip] =3D File.read(f)
end

//Anders


--=20
=2E . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
=2E Anders Engstr=F6m (e-mail address removed)
=2E http://www.gnejs.net PGP-Key: ED010E7F
=2E [Your mind is like an umbrella. It doesn't work unless you open it.] =
=20


--cNdxnHkX5QqsyA0e
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFBEbyquNLLbe0BDn8RAnuaAJ9KdNz4zh54+MhrfA0daweNtZhomgCgj2eX
3yWy8dQW5YFabVrunVTsk1Y=
=QbS3
-----END PGP SIGNATURE-----

--cNdxnHkX5QqsyA0e--
 
R

Robert Klemme

Matthew Margolis said:
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

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top