# Strange YAML Problem: Sometimes can't open file

**URL:** <https://www.ruby-forum.com/t/strange-yaml-problem-sometimes-cant-open-file/144140>\
**Category:** Rails\
**Created:** [July 25, 2008, 8:17pm UTC](https://www.ruby-forum.com/t/strange-yaml-problem-sometimes-cant-open-file/144140 "2008-07-25T20:17:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [July 25, 2008, 8:17pm UTC](https://www.ruby-forum.com/t/strange-yaml-problem-sometimes-cant-open-file/144140/1 "2008-07-25T20:17:56Z")

</div>

This is a weird problem that only happens once in a while. Most of the  
time there is no problem, but occassionally, I cannot open a file.  
Here’s some of the code.

############# ACTION 1 #########################  
require ‘fastercsv’  
require ‘tmpdir’  
require ‘yaml’

# blah blah

#blah

@parsed\_file = FasterCSV.parse(params[:dump][:file])

# blah

@temp\_file = Tempfile.new(“temporary\_csv\_file”)  
@temp\_file.write(YAML::dump(@parsed\_file))  
@temp\_file.close  
################################################

In the view, there is a button to import the file after it has been  
checked.  
############ VIEW 1 ###########  
\<%= button\_to “Import Data”, :action =\> :import\_data, :path =\>  
@temp\_file.path %\>  
######################

######### ACTION “IMPORT DATA” ##########  
require ‘yaml’  
begin  
@csv\_file = YAML::load(File.open(params[:path]))  
rescue  
flash[:error] = “Could not import links; error opening temporary  
file.”  
redirect\_to :action =\> :upload and return  
end  
###########################

Most of the time, there is no problem opening the file. But sometimes,  
the file fails to open. It’s weird because it only happens sometimes;  
does anyone have any suggestions on this?

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [July 25, 2008, 8:22pm UTC](https://www.ruby-forum.com/t/strange-yaml-problem-sometimes-cant-open-file/144140/2 "2008-07-25T20:22:56Z")

</div>

> #blah  
> ############ VIEW 1 ###########  
> file."  
> redirect\_to :action =\> :upload and return  
> end  
> ###########################
> 
> Most of the time, there is no problem opening the file. But  
> sometimes,  
> the file fails to open. It’s weird because it only happens sometimes;  
> does anyone have any suggestions on this?

Same file randomly works? Or are the files slightly different? Trap  
the error and spit it out instead of hiding it in the rescue. That  
might help figure out the cause…

Might be a UTF8 garbled-character issue. Maybe it’s not a valid CSV,  
maybe it’s unterminated…

-philip

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [July 25, 2008, 8:49pm UTC](https://www.ruby-forum.com/t/strange-yaml-problem-sometimes-cant-open-file/144140/3 "2008-07-25T20:49:45Z")

</div>

> Same file randomly works? Or are the files slightly different? Trap  
> the error and spit it out instead of hiding it in the rescue. That  
> might help figure out the cause…
> 
> Might be a UTF8 garbled-character issue. Maybe it’s not a valid CSV,  
> maybe it’s unterminated…
> 
> -philip

It can be the same file each time. Very weird. Here is the error it  
catches:

rescued #\<Errno::ENOENT: No such file or directory -  
/var/folders/kp/kppgyL-IHt03oEOrzn+sBk+++TM/-Tmp-/temporary\_csv\_file.680.0\>

And here is the path of a working one:  
“/var/folders/kp/kppgyL-IHt03oEOrzn+sBk+++TM/-Tmp-/temporary\_csv\_file.680.0”

Is this just a bug with YAML?
