# Problems with opening and reading data from a file

**URL:** <https://www.ruby-forum.com/t/problems-with-opening-and-reading-data-from-a-file/155072>\
**Category:** Ruby\
**Created:** [December 7, 2008, 12:53pm UTC](https://www.ruby-forum.com/t/problems-with-opening-and-reading-data-from-a-file/155072 "2008-12-07T12:53:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [December 7, 2008, 12:53pm UTC](https://www.ruby-forum.com/t/problems-with-opening-and-reading-data-from-a-file/155072/1 "2008-12-07T12:53:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [December 7, 2008, 2:26pm UTC](https://www.ruby-forum.com/t/problems-with-opening-and-reading-data-from-a-file/155072/2 "2008-12-07T14:26:17Z")

</div>

On Sun, Dec 7, 2008 at 12:46 PM, Stuart C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## Settings\user\Desktop\terms.txt (Er if line =~ term
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

I am not sure \ works, try simple /.  
And I am sure the file exists and is readable.  
Another topic  
instead of  
o = 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  
block.

HTH  
R.

–  
Il computer non è una macchina intelligente che aiuta le persone  
stupide, anzi, è 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
