title: Frequent "No such file or directory" while saving files via Paperclip
body: <p>I feel like this will end in a facepalm moment but I've been banging on my head on it for too long.</p>
  
  <p>I have a Rails seed.rb file that gets all the files from a specific directory creating a new object for each file and saving the file via Paperclip:</p>
  
  <pre><code>Dir["./**/*.jpg"].each do |f|
    ...
    p = Picture.new
    File.open(f, 'r') { |photo_file| p.photo = photo_file }
    p.save!
    ....
  end
  </code></pre>
  
  <p>where <code>photo</code> is the Paperclip-assigned attribute (picture.rb):</p>
  
  <pre><code>has_attached_file :photo,
                    :styles =&gt; { :medium =&gt; "500x500&gt;", :thumb =&gt; "100x100#" },
                    :processors =&gt; [:rotator]
  </code></pre>
  
  <p>My problem is after some number of files (some times 50, some times 2) the script exits with the following error:</p>
  
  <pre><code>No such file or directory - /var/folders/oD/oDq1WD11EEaXmfi8VfNvfE+++TM/-Tmp-/stream,22423,0,22423,0
  /Users/patgeorge/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/fileutils.rb:1407:in `stat'
  /Users/patgeorge/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/fileutils.rb:1407:in `block in fu_each_src_dest'
  /Users/patgeorge/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/fileutils.rb:1423:in `fu_each_src_dest0'
  /Users/patgeorge/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/fileutils.rb:1405:in `fu_each_src_dest'
  /Users/patgeorge/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/fileutils.rb:504:in `mv'
  /Users/patgeorge/.rvm/gems/ruby-1.9.2-head@rails3/bundler/gems/paperclip-61f74de14812cabc026967a2b2c3ca8cbd2eed69-master/lib/paperclip/storage.rb:42:in `block in flush_writes'
  </code></pre>
  
  <p>I thought maybe I wasn't closing the file but according to the <a href="http://ruby-doc.org/core/classes/IO.html#M002239" rel="nofollow noreferrer">Ruby IO docs</a> using the block from of <code>open</code> will close the file.</p>
  
  <p>Obviously I don't see myself having to run this often so it's not a huge problem.  It's just frustrating and confusing.</p>
  
  <p>I'm running Ruby 1.9.2 r28142, Rails 3.0.0.beta4, and Paperclip 2.3.3.</p>
  
  <p><strong>Additional:</strong></p>
  
  <p>Attempting Winfield's suggestion my code block now looks like this:</p>
  
  <pre><code>Dir["./**/*.jpg"].each do |f|
    ...
    File.open(f, 'r') do |photo_file|
      p = Picture.new
      p.photo = photo_file
      p.save!
    end
    ...
  end
  </code></pre>
  
  <p>Still getting the error periodically, though.</p>
  
  <p><strong>Still more info</strong>:</p>
  
  <p>I noticed that when I first run my script it's able to do a large amount of files (12 or so).  As I continue to run it the number decreases to where I can only do 2 at a time.  I'm not sure what I'm doing to make it "reset" and process more.  But I imagine that's the key.</p>