# Basic problems with accessing a file

**URL:** <https://www.ruby-forum.com/t/basic-problems-with-accessing-a-file/67873>\
**Category:** Ruby\
**Created:** [September 7, 2006, 4:07pm UTC](https://www.ruby-forum.com/t/basic-problems-with-accessing-a-file/67873 "2006-09-07T16:07:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![james-calivar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@james-calivar](https://www.ruby-forum.com/u/james-calivar)\
**Post date:** [September 7, 2006, 4:07pm UTC](https://www.ruby-forum.com/t/basic-problems-with-accessing-a-file/67873/1 "2006-09-07T16:07:08Z")

</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)  
puts “Exists.”  
else  
puts “Does not exist.”  
end

# Process file

File.open(filename, “r”) do |input\_file|  
while line = input\_file.gets  
puts line if line =~ /^[O] [/  
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)  
from RequirementsExtractor.rb:35:in `open’  
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:** ![james-calivar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@james-calivar](https://www.ruby-forum.com/u/james-calivar)\
**Post date:** [September 7, 2006, 4:13pm UTC](https://www.ruby-forum.com/t/basic-problems-with-accessing-a-file/67873/2 "2006-09-07T16:13:09Z")

</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:** ![james-calivar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@james-calivar](https://www.ruby-forum.com/u/james-calivar)\
**Post date:** [September 7, 2006, 4:17pm UTC](https://www.ruby-forum.com/t/basic-problems-with-accessing-a-file/67873/3 "2006-09-07T16:17:10Z")

</div>

> > > > > “J” == James C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

J\> puts ("Please enter the name of the file you wish to process: ")  
J\> filename = gets

filename = gets.chomp # to remove the newline which is at the end of  
the  
# string

J\> puts ("File to be processed: " + filename)

–

Guy Decoux

---

<div class="post-metadata">

**Author:** ![james-calivar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@james-calivar](https://www.ruby-forum.com/u/james-calivar)\
**Post date:** [September 7, 2006, 4:31pm UTC](https://www.ruby-forum.com/t/basic-problems-with-accessing-a-file/67873/4 "2006-09-07T16:31:15Z")

</div>

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

James
