title: How to download scrapy images in a dyanmic folder based on
body: <p>I'm trying to override default path <code>full/hash.jpg</code> to <code>&lt;dynamic&gt;/hash.jpg</code>, I've tried <a href="https://stackoverflow.com/questions/27386509/how-to-download-scrapy-images-in-a-dyanmic-folder">How to download scrapy images in a dyanmic folder</a> using following code:</p>
  
  <pre><code>def item_completed(self, results, item, info):
  
      for result in [x for ok, x in results if ok]:
          path = result['path']
          # here we create the session-path where the files should be in the end
          # you'll have to change this path creation depending on your needs
          slug = slugify(item['category'])
          target_path = os.path.join(slug, os.path.basename(path))
  
          # try to move the file and raise exception if not possible
          if not os.rename(path, target_path):
              raise DropItem("Could not move image to target folder")
  
      if self.IMAGES_RESULT_FIELD in item.fields:
          item[self.IMAGES_RESULT_FIELD] = [x for ok, x in results if ok]
      return item
  </code></pre>
  
  <p>but I get:</p>
  
  <pre><code>Traceback (most recent call last):
      File "/home/user/.venv/sepid/lib/python2.7/site-packages/twisted/internet/defer.py", line 577, in _runCallbacks
      current.result = callback(current.result, *args, **kw)
      File "/home/user/.venv/sepid/lib/python2.7/site-packages/twisted/internet/defer.py", line 839, in _cbDeferred
      self.callback(self.resultList)
      File "/home/user/.venv/sepid/lib/python2.7/site-packages/twisted/internet/defer.py", line 382, in callback
      self._startRunCallbacks(result)
      File "/home/user/.venv/sepid/lib/python2.7/site-packages/twisted/internet/defer.py", line 490, in _startRunCallbacks
      self._runCallbacks()
      --- &lt;exception caught here&gt; ---
      File "/home/user/.venv/sepid/lib/python2.7/site-packages/twisted/internet/defer.py", line 577, in _runCallbacks
      current.result = callback(current.result, *args, **kw)
      File "/home/user/Projects/sepid/scraper/scraper/pipelines.py", line 44, in item_completed
      if not os.rename(path, target_path):
      exceptions.OSError: [Errno 2] No such file or directory
  </code></pre>
  
  <p>I don't know what's wrong, also is there any other way to change the path? Thanks</p>