title: Yaml : Found unexpected end of stream
body: <p>I have written a python code to generate a YAML file(called <code>targets.yml</code>), which is read by a popular monitoring application called prometheus. The prometheus successfully reads the YAML file and its contents but it also throws an error in the log like below. </p>
  
  <pre><code>level=error msg="Error reading file "/var/targets/targets.yml": yaml: 
  line 218: found unexpected end of stream" source="file.go:199" 
  </code></pre>
  
  <p>I am unable to get rid of this error, though I close the YAML file appropriately and below is the code for that:-</p>
  
  <pre><code>while True:
  
      create()
      with open('/var/targets/targets.yml', 'w') as output:
          print "opened the file to write"
          i=0
          for item in result:
              if(item != None and item['status'] == "ACTIVE"):
                 print item['domains']['partner']
                 print item['status']
                 output.write("\n\n")
                 output.write("- targets: ['" + "https://" + item["domains"]["agency"] + "']\n")
                 output.write("  labels:\n")
                 output.write("    alias: " + item["alias"])
                 foo=item["name"]
             #print foo
                 if isinstance(foo,basestring):
                    foo=foo.encode('utf8')
                 else:
                    foo=unicode(foo).encode('utf8')
  output.close()
  print("Waiting for 300 seconds, before relooping")
  time.sleep(100)
  </code></pre>
  
  <p>Also I do not think that my file extension makes any difference. Can somebody please suggest?</p>