title: Scrapy Item Pipelines and file naming
body: <p>I am trying to name a pipeline output file with the spider name and a date.
  I've written a small piece of code that gets called to date the file.
  The issue is with the spider name.
  Below are two methods. Both work, but I'd like to understand the nuances.
  The first method generates a suggestion from PyCharm that I don't understand especially because it follows the example from <a href="https://docs.scrapy.org/en/latest/topics/item-pipeline.html#write-items-to-a-json-file" rel="nofollow noreferrer">scrapy docs</a>.</p>
  
  <p><strong>First method:</strong></p>
  
  <pre><code># pipelines.py
  
  # Cannot add spider as input to class Pipeline
  class CsvPipeline(object):
  
      def open_spider(self, spider):
          # Call to put file in correct directory
          redefine_dir(spider, file_type='csv')
          # Call to name file correctly
          name = dated_filename(spider, '.csv')
          # PyCharm insists the following two lines should be in __init__; Why?
          # Only seem to be able to have spider as input to open_spider and not the class
          self.file = open(name, 'wb')
          self.exporter = CsvItemExporter(self.file)
          self.exporter.start_exporting()
  
      def close_spider(self):
          self.exporter.finish_exporting()
          self.file.close()
  
      def process_item(self, item):
          self.exporter.export_item(item)
          return item
  </code></pre>
  
  <p><strong>Second method:</strong></p>
  
  <pre><code>class JsonPipeline(object):
      def __init__(self):
          # As I cannot figure out how spider is an input to __init__ I have to create a temporary file
          # This temporary file is renamed later.
          print('Current working directory:', os.getcwd())
          if os.getcwd() == 'C:\\PycharmProjects\\ABC\\abc\\run':
              os.chdir('..')
          elif os.getcwd() == 'C:\\PycharmProjects\\ABC':
              os.chdir('abc')
          print('Current working directory now:', os.getcwd())
          # Temporary file created
          self.file = open('data/raw.json', 'wb')
          self.exporter = JsonItemExporter(self.file)
          self.exporter.start_exporting()
  
      def close_spider(self, spider):
          self.exporter.finish_exporting()
          self.file.close()
          new_name = dated_filename(spider, '.json')
          print('Current working directory:', os.getcwd())
          if os.getcwd() == 'C:\\PycharmProjects\\ABC\\abc\\run':
              os.chdir('..')
          elif os.getcwd() == 'C:\\PycharmProjects\\ABC':
              os.chdir('abc')
          print('Current working directory now:', os.getcwd())
          # Rename file to dated filename
          rename('data/raw.json', new_name)
  
      def process_item(self, item):
          self.exporter.export_item(item)
          return item
  </code></pre>
  
  <ol>
  <li>Is it possible to include spider as input to Pipeline class? If so, how?</li>
  <li>Is it possible to include spider as input to Pipeline class <strong>init</strong>? If so, how?</li>
  <li>Why does PyCharm insist I should put the self.file and self.exporter under <strong>init</strong>?</li>
  <li>Any better ideas?</li>
  </ol>