title: Python Selenium Scraper error - ValueError: I/O operation on closed file
body: <p>I am trying to run the following python code</p>
  <p>Technology: Python, Selenium scraper<br />
  Device: Windows device</p>
  <p>Getting error......</p>
  <pre><code>Traceback (most recent call last): 
  File &quot;scraper.py&quot;, line 35, in for row in cp_url: 
  ValueError: I/O operation on closed file.
  </code></pre>
  <pre><code>#!/usr/bin/python3
  # Description: The Python code below will search selenium in Google.
  import time
  import csv
  import os
  
  
  from selenium import webdriver
  from selenium.webdriver.common.keys import Keys
  
  #EVERYTIME CHANGE THE DRIVER PATH TO THE CHROME DRIVER FOR LATEST CHROME VERSION
  driver = webdriver.Chrome(
      executable_path=&quot;D:\chromedriver.exe&quot;)
  
  options = webdriver.ChromeOptions()
  options.add_experimental_option('excludeSwitches', ['enable-logging'])
  
  contents = []
  
  filePath = 'output1.csv'
  # As file at filePath is deleted now, so we should check if file
  # exists or not not before deleting them
  if os.path.exists(filePath):
      os.remove(filePath)
  else:
      print(&quot;Can not delete the file as it doesn't exists&quot;)
  
  f = open(&quot;output1.csv&quot;, &quot;a&quot;)
  f.write(&quot;website,&quot; + &quot;htmltag,&quot; + &quot;type,&quot; + &quot;id,&quot; + &quot;classname,&quot; + &quot;for,&quot; + &quot;href,&quot; + &quot;alt,&quot; + &quot;type,&quot; + &quot;src,&quot;
  + &quot;name,&quot; + &quot;width,&quot; + &quot;height,&quot; + &quot;data-src,&quot;+ 'inner-text,' + 'action,' + 'value,' + &quot;\n&quot;)
  
  
  with open('inputLinks1.csv', 'rt') as cp_csv:
   cp_url = csv.reader(cp_csv)
  for row in cp_url:
          links = row[0]
          contents.append(links)
          driver.get(links)
          with open('xpathtags.csv', 'rt') as cp2_csv:
              cp_url2 = csv.reader(cp2_csv)
              for row1 in cp_url2:
                  print(row[0])
                  (xtype, xpathtext) = row1[0].split(';') 
                  print(xtype, xpathtext)
                  contents.append(xtype)
                  contents.append(xpathtext)
                  elems = driver.find_elements_by_xpath(xpathtext)
                  for elem in elems:
                      f = open('output1.csv', 'a', encoding='utf-8')
                      f.write( links + &quot;, &quot;+ xtype + &quot;,&quot; 
                          + str(elem.get_attribute('type')) + ', '
                          + str(elem.get_attribute('id')) + ', '
                          + str(elem.get_attribute('class')) + ', '
                          + str(elem.get_attribute('for')) + ', '
                          + str(elem.get_attribute('href')) + ', '
                          + str(elem.get_attribute('alt')) + ', '                        
                          + str(elem.get_attribute('type')) + ', '
                          + str(elem.get_attribute('src')) + ', '
                          + str(elem.get_attribute('name')) + ', '
                          + str(elem.get_attribute('width')) + ', '
                          + str(elem.get_attribute('height')) + ', '
                          + str(elem.get_attribute('data-src')) + ', '
                          + str(elem.get_attribute('innerText').strip()) + ', '
                          + str(elem.get_attribute('action')) + ', '
                           + str(elem.get_attribute('value')) + ', '
  
                          + '\n')
                     
                      f.close()  
  
  
  driver.close()
  </code></pre>
  <p>I am using the following CSV files</p>
  <p>A) inputlinks1.csv</p>
  <pre><code>www.flipkart.com
  www.ebay.com
  </code></pre>
  <p>B) xpathtags.csv</p>
  <pre><code>Link;//a[@href]
  Button;//button
  Image;//img
  Heading1;//h1
  Heading2;//h2
  Heading3;//h3
  Heading4;//h4
  </code></pre>
  <p>C) <code>Output.csv</code> is a blank file</p>
  <p>I am getting the following error</p>
  <pre><code>Traceback (most recent call last):
    File &quot;scraper.py&quot;, line 35, in &lt;module&gt;
      for row in cp_url:
    ValueError: I/O operation on closed file.
  </code></pre>