Forum: Ruby Error: File not found.

Posted by Adam Kennedy (adamk__)
on 2013-01-16 15:07
Hello guys and gals.

This has been bugging me for three hours! I keep receiving the error:
File not found. Even though I have placed it in the correct location. I
have no idea why it is doing it as I feel it should be working. Here is
the code:

#!/media/Apache/ruby/bin/ruby
#!/usr/local/bin/ruby

#usr_array = File.open("../logs/usernames_log", "r").read.split("\n")
Culprate = File.open("../products/culprate.txt", "r").read.split("\n")

puts "Content-type: text/html\n\n"

puts "<html>"
puts "<head>"
puts "<title>Culprate Nightmares in reality</title>"
puts "</head>"
puts "<body>"
puts "<p>Name : Culprate [0]</p>"
puts "<p>Description : Culprate [1]</p>"
puts "<p>Price : Culprate [2]</p>"
puts "<p>Item Number : Culprate [3]</p>"
puts "</body>"
puts "</html>"

And the error:
5:in `initialize': No such file or directory - ../products/culprate.txt
(Errno::ENOENT)

The user_array does exactly the same thing in another script and it has
been working perfectly. Hope someone can help!

Thanks
Adam K
Posted by Adam Kennedy (adamk__)
on 2013-01-16 15:19
Also the culprate.txt file is placed in

htdocs/products/culprate.txt
Posted by Panagiotis Atmatzidis (Guest)
on 2013-01-16 15:41
(Received via mailing list)
Hello,

It's better if you start using file paths like this:

file = File.join(File.dirname(__FILE__), 'dir1', 'dir2', 'file.txt')

Generally it's a good idea to learn how to use "File.dirname(__FILE__)" 
which gives the directory
of the current file. Play with it a bit and you'll figure it out!

Best Regards,

On 16 Ιαν 2013, at 15:07 , Adam Kennedy <lists@ruby-forum.com> wrote:

> #usr_array = File.open("../logs/usernames_log", "r").read.split("\n")
> puts "<p>Description : Culprate [1]</p>"
> been working perfectly. Hope someone can help!
>
> Thanks
> Adam K
>
> --
> Posted via http://www.ruby-forum.com/.
>


Panagiotis (atmosx) Atmatzidis

email:  atma@convalesco.org
URL:  http://www.convalesco.org
GnuPG ID: 0xE736C6A0
gpg --keyserver x-hkp://pgp.mit.edu --recv-keys 0xE736C6A0
Posted by Adam Kennedy (adamk__)
on 2013-01-16 15:47
Thanks man! Ive have a look at it now :)

Adam K
Posted by Adam Kennedy (adamk__)
on 2013-01-16 16:47
Still not working. I understand it brings up the text file directory but 
it still says that it cannot be found.

Also with the information from the text file I am putting it into an 
array then displaying the array information. Could this be effecting the 
code?

Adam K
Posted by Wayne Brisette (Guest)
on 2013-01-16 17:25
(Received via mailing list)
Adam:

Can anybody read the contents in that directory? Almost sounds like 
there is
some sort of read permissions error.

Wayne
Posted by Jeremy Bopp (Guest)
on 2013-01-16 17:45
(Received via mailing list)
On 01/16/2013 10:24 AM, Wayne Brisette wrote:
> Adam:
>
> Can anybody read the contents in that directory? Almost sounds like there is
> some sort of read permissions error.

Another possibility is that the relative file path being used in the
code is failing to find the intended file because the current working
directory of the script is not in the correct place.  Try printing the
output of File.expand_path for the file path to see if it resolves to
the place you expect.

-Jeremy
Posted by Hassan Schroeder (Guest)
on 2013-01-16 17:45
(Received via mailing list)
On Wed, Jan 16, 2013 at 6:07 AM, Adam Kennedy <lists@ruby-forum.com> 
wrote:

> This has been bugging me for three hours! I keep receiving the error:
> File not found. Even though I have placed it in the correct location. I
> have no idea why it is doing it as I feel it should be working.

Ignoring all the other issues with your code, the likelihood is that
the process being run is *not* relative to where your program file
is stored.

Include the result of Dir.getwd in your program and find out. :-)
Posted by Adam Kennedy (adamk__)
on 2013-01-17 15:20
Hi guys, thanks for all of your replies. After about 6 hours, I managed 
to get it working by simply changing the way the File.open code was 
being executed. Very very strange but glad its working now :)

Adam K
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.