# Basic problems with accessing a file

**URL:** <https://rubytalk.org/t/basic-problems-with-accessing-a-file/30746>\
**Category:** ruby-talk\
**Created:** [7 September 2006 14:05 UTC](https://rubytalk.org/t/basic-problems-with-accessing-a-file/30746 "2006-09-07T14:05:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Calivar](https://avatars.discourse-cdn.com/v4/letter/j/977dab/32.png) [@James\_Calivar](https://rubytalk.org/u/James_Calivar)\
**Post date:** [7 September 2006 14:05 UTC](https://rubytalk.org/t/basic-problems-with-accessing-a-file/30746/1 "2006-09-07T14:05:20Z")

</div>

Hello,

I'm confused. I'm attempting to query a user for a filename to be  
processed, and then I want to open that file for parsing. However, I'm  
getting an error that the file does not exist. Hopefully someone can  
help?

Here is my code:

# Get filename  
puts ("Please enter the name of the file you wish to process: ")  
filename = gets  
puts ("File to be processed: " + filename)  
if File.exist?(filename)  
&nbsp;&nbsp;puts "Exists."  
else  
&nbsp;&nbsp;puts "Does not exist."  
end

# Process file  
File.open(filename, "r") do |input\_file|  
&nbsp;&nbsp;while line = input\_file.gets  
&nbsp;&nbsp;&nbsp;&nbsp;puts line if line =~ /^[O] \[/  
&nbsp;&nbsp;end  
end

Running this (Windows XP) produces the following:

C:\Documents and Settings\jcalivar\Desktop\>ruby  
RequirementsExtractor.rb  
Please enter the name of the file you wish to extract requirements  
from:  
fred.txt  
File to be processed: fred.txt  
Does not exist.  
RequirementsExtractor.rb:35:in `initialize': No such file or directory  
- fred.txt (Errno::ENOENT)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from RequirementsExtractor.rb:35:in `open'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from RequirementsExtractor.rb:35

I can't figure this out - because "fred.txt" certainly \*does\* exist on  
my desktop. Help! 🙂

James

---

<div class="post-metadata">

**Author:** ![Peter\_Hickman](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@Peter\_Hickman](https://rubytalk.org/u/Peter_Hickman)\
**Post date:** [7 September 2006 14:11 UTC](https://rubytalk.org/t/basic-problems-with-accessing-a-file/30746/2 "2006-09-07T14:11:57Z")

</div>

Is the gets giving you a string with a \n on the end?

You might have to chomp it first.

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [7 September 2006 14:15 UTC](https://rubytalk.org/t/basic-problems-with-accessing-a-file/30746/3 "2006-09-07T14:15:33Z")

</div>

> puts ("Please enter the name of the file you wish to process: ")  
> filename = gets  
> &nbsp;&nbsp;&nbsp;  
> &nbsp;&nbsp;&nbsp;filename = gets.chomp # to remove the newline which is at the end of the  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# string

> **···**
>
> > puts ("File to be processed: " + filename)
> 
> --
> 
> Guy Decoux

---

<div class="post-metadata">

**Author:** ![James\_Calivar](https://avatars.discourse-cdn.com/v4/letter/j/977dab/32.png) [@James\_Calivar](https://rubytalk.org/u/James_Calivar)\
**Post date:** [7 September 2006 14:30 UTC](https://rubytalk.org/t/basic-problems-with-accessing-a-file/30746/4 "2006-09-07T14:30:17Z")

</div>

I tried gets.chomp instead of gets and it worked! Thanks.

James

ts wrote:

> **···**
>
> > \> puts ("Please enter the name of the file you wish to process: ")  
> > \> filename = gets
> > 
> > &nbsp;&nbsp;&nbsp;filename = gets.chomp # to remove the newline which is at the end of the  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# string
> > 
> > \> puts ("File to be processed: " + filename)
> > 
> > --
> > 
> > Guy Decoux
