Problems with opening and reading data from a file

S

Stuart Clarke

I am trying to read keywords and phrases from a .txt file and run these
words against a given directory structure. I seem to be having a few
problems, when I run my code i get the following error:

C:\Documents and Settings\user\Desktop>keywordsearch.rb "C:\Documents
and Sett
ings\user"
C:/Documents and Settings/user/Desktop/keywordsearch.rb:21:in
`initialize': No
such file or directory - C:\Documents and
Settings\user\Desktop\terms.txt (Er
rno::ENOENT)

I cannot see what is wrong with my code see below:

File.open(curPath) do |file|
file.each do |line|
o = File.open("C:\\Documents and
Settings\\user\\Desktop\\terms.txt")
terms = o.readlines
terms.each do |term|
if line =~ term
puts "#{curPath}"
end
end
o.close
end
end

Any ideas, I really appreciate the guidance.

Regards
 
R

Robert Dober

I am trying to read keywords and phrases from a .txt file and run these
words against a given directory structure. I seem to be having a few
problems, when I run my code i get the following error:

C:\Documents and Settings\user\Desktop>keywordsearch.rb "C:\Documents
and Sett
ings\user"
C:/Documents and Settings/user/Desktop/keywordsearch.rb:21:in
`initialize': No
such file or directory - C:\Documents and
Settings\user\Desktop\terms.txt (Er
rno::ENOENT)

I cannot see what is wrong with my code see below:

File.open(curPath) do |file|
file.each do |line|
o =3D File.open("C:\\Documents and
Settings\\user\\Desktop\\terms.txt")
terms =3D o.readlines
terms.each do |term|
if line =3D~ term
puts "#{curPath}"
end
end
o.close
end
end

Any ideas, I really appreciate the guidance.

Regards
I am not sure \\ works, try simple /.
And I am sure the file exists and is readable.
Another topic
instead of
o =3D File.open
...
o.close
you might prefer
File.open do |o|
...
end
which will assure o is closed *even* if an exception is raised in your bloc=
k.

HTH
R.


--=20
Il computer non =E8 una macchina intelligente che aiuta le persone
stupide, anzi, =E8 una macchina stupida che funziona solo nelle mani
delle persone intelligenti.
Computers are not smart to help stupid people, rather they are stupid
and will work only if taken care of by smart people.

Umberto Eco
 

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,013
Latest member
KatriceSwa

Latest Threads

Top