title: Why python script has to be run twice?
body: <p>I wrote this python script to web scrape data and to print the output to a separate file.
  The 'refID.txt' file has a list of IDs and for each ID, data has to be extracted from a site. The output will be printed to the 'output.txt' file.
  Here is my code;</p>
  <pre><code>import urllib
  import re
  
  referencefile = open(&quot;refID.txt&quot;)
  
  IDlist = referencefile.read()
  
  refIDlist = IDlist.split(&quot;\n&quot;)
  
  f = open(&quot;output.txt&quot;, 'w')
  
  i=0
  while i&lt;len(refIDlist):
    url = &quot;http://www.ncbi.nlm.nih.gov/clinvar/variation/&quot;+refIDlist[i]
    htmlfile = urllib.urlopen(url)
    htmltext = htmlfile.read()
    regex = '&lt;dt&gt;Variant type:&lt;/dt&gt;&lt;dd&gt;(.+?)&lt;/dd&gt;'
    pattern = re.compile(regex)
    Vtype = re.findall(pattern,htmltext)
    vt = Vtype[0]
    printing = &quot;Variation type of &quot; + refIDlist[i] + &quot; is &quot; + str(vt)
    print &gt;&gt; f, printing
    i+=1  
  </code></pre>
  <p>My problem is, for the output to be printed in the 'output.txt' file, the code has to be run twice. If the script is run once, no output will be printed. But if the code is run for the second time, the output will be printed.
  How can I print the output when the code is run only once?</p>