# Mechanize error, File doesn't exist -- but it DOES!

**URL:** <https://www.ruby-forum.com/t/mechanize-error-file-doesnt-exist-but-it-does/149858>\
**Category:** Ruby\
**Created:** [September 30, 2008, 4:43pm UTC](https://www.ruby-forum.com/t/mechanize-error-file-doesnt-exist-but-it-does/149858 "2008-09-30T16:43:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [September 30, 2008, 4:43pm UTC](https://www.ruby-forum.com/t/mechanize-error-file-doesnt-exist-but-it-does/149858/1 "2008-09-30T16:43:30Z")

</div>

I am using Mechanize to fill out a file upload form for an image file.  
My script is telling me that an image file DOESN’T exist:

/opt/local/lib/ruby/gems/1.8/gems/mechanize-0.7.2/lib/www/mechanize/form.rb:273:in  
`initialize’: No such file or directory -  
/Users/dbit/Sites/fuseme/fuseme\_rails/app/models/scrapers/web20/web20\_logos/evenspacer.gif  
(Errno::ENOENT)

Does anybody have a clue as to why it would happen?

When I use the UNIX ‘ls’ command to ls the file name using the absolute  
path name of the image “evenspacer.gif” (as seen in the above error  
message), the UNIX prompt lists that file, meaning that it’s there!

I thought it might be because the file name was named in a different  
character set, but I changed the XML file from which the name of the  
file was being taken and retyped in both the XML file and renamed the  
file by hand using my keyboard( thereby overwriting whatever character  
set it was previously represented in).

Does anybody have any ideas as to what’s going on here? I’m a bit  
stumped!

Thank you!  
David 🙂

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [September 30, 2008, 5:00pm UTC](https://www.ruby-forum.com/t/mechanize-error-file-doesnt-exist-but-it-does/149858/2 "2008-09-30T17:00:37Z")

</div>

On Sep 30, 10:42 am, David B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am using Mechanize to fill out a file upload form for an image file.  
> My script is telling me that an image file DOESN’T exist:

Can we see the offending lines of the script? Or do you want wild  
guesses?

Have you seen the EXAMPLES file from the docs which has sample upload  
code?

– Mark.

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [September 30, 2008, 5:06pm UTC](https://www.ruby-forum.com/t/mechanize-error-file-doesnt-exist-but-it-does/149858/3 "2008-09-30T17:06:50Z")

</div>

If you won’t post the script , the solution is to step through the code  
and see what happens .

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [October 1, 2008, 10:44am UTC](https://www.ruby-forum.com/t/mechanize-error-file-doesnt-exist-but-it-does/149858/4 "2008-10-01T10:44:01Z")

</div>

One possible source of error might be the user that your rails script is  
running as. If your normal user (as you were with the one liner test) is  
not the same as the user assigned to run the rails script then it might  
be that the rails script / user does not have permission to read the  
file or some part of the path (to be sure you could set all files and  
directories to a+r and all directories to a+x on the path to the file in  
question).

Thats all that I can think of.

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [October 1, 2008, 10:30am UTC](https://www.ruby-forum.com/t/mechanize-error-file-doesnt-exist-but-it-does/149858/5 "2008-10-01T10:30:42Z")

</div>

Here is basically what’s running in the script:

```
file_path = "web20_logos/" + "evenspacer.gif"
puts "rel path: " + file_path
puts "exists?: " + File.exists?( file_path ).to_s
puts "expanded path: " + File.expand_path( file_path )
puts "exists?: " + File.exists?( File.expand_path( file_path ) 

```

).to\_s

yields:

exists?: false  
expanded path:  
/Users/dbit/Sites/fuseme/fuseme\_rails/app/models/scrapers/web20/web20\_logos/evenspacer.gif  
exists?: false  
/opt/local/lib/ruby/gems/1.8/gems/mechanize-0.8.3/lib/www/mechanize/form.rb:294:in  
`initialize': No such file or directory - /Users/dbit/Sites/fuseme/fuseme_rails/app/models/scrapers/web20/web20_logos/evenspacer.gif (Errno::ENOENT) from /opt/local/lib/ruby/gems/1.8/gems/mechanize-0.8.3/lib/www/mechanize/form.rb:294:in`open’  
from  
/opt/local/lib/ruby/gems/1.8/gems/mechanize-0.8.3/lib/www/mechanize/form.rb:294:in  
`file_to_multipart' from /opt/local/lib/ruby/gems/1.8/gems/mechanize-0.8.3/lib/www/mechanize/form.rb:200:in`request\_data’  
from  
/opt/local/lib/ruby/gems/1.8/gems/mechanize-0.8.3/lib/www/mechanize/form.rb:200:in  
`each' from /opt/local/lib/ruby/gems/1.8/gems/mechanize-0.8.3/lib/www/mechanize/form.rb:200:in`request\_data’  
from  
/opt/local/lib/ruby/gems/1.8/gems/mechanize-0.8.3/lib/www/mechanize.rb:380:in  
`post_form' from /opt/local/lib/ruby/gems/1.8/gems/mechanize-0.8.3/lib/www/mechanize.rb:324:in`submit’  
from web20.rb:47:in `submit\_new\_thing’  
from web20.rb:90

But when I type it at the command line:

ruby -e ‘puts (File.exist? “web20\_logos/evenspacer.gif”).to\_s’

# It yields true…

Very weird.

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [October 1, 2008, 4:19pm UTC](https://www.ruby-forum.com/t/mechanize-error-file-doesnt-exist-but-it-does/149858/6 "2008-10-01T16:19:01Z")

</div>

There was a space at the end of the file name in the XML file, but not  
in the actual file name . . . . . _sigh_

Peter H. wrote:

> One possible source of error might be the user that your rails script is  
> running as. If your normal user (as you were with the one liner test) is  
> not the same as the user assigned to run the rails script then it might  
> be that the rails script / user does not have permission to read the  
> file or some part of the path (to be sure you could set all files and  
> directories to a+r and all directories to a+x on the path to the file in  
> question).
> 
> Thats all that I can think of.
