title: How to close an xml file after overwriting an attribute in python
body: <p>I want to change an attributes value in an xmlfile, from 0 to 1 or vice versa. I have two methods of writing to an xmlfile, the first (xml_to_bin) leaves the file open and the second (xml_to_bin2) seems to remove all the contents of the xmlfile and gives a long traceback.</p>
  <p>I need to be able to access the xml file many times in a test so when I save a value and have to reread that value later I need to know it has been changed and saved.</p>
  <p>I have seen two similar questions, one helped me get this far.</p>
  <p><strong>Code</strong></p>
  <pre><code>import xml.etree.ElementTree as ET
  import time
  
  if __name__ != &quot;__main__&quot;: #for when this module is imported
      DropWatch = None
  
  def get_bin_gs_xml_setting(xmlfile_path):
      tree = ET.parse(xmlfile_path)
      root = tree.getroot()
      isBinary = None
      #ET.dump(tree)# prints the xml file to console,shows xml indentation
      print('\n')
      for elm in root.findall(&quot;./Common/ForceToBinary&quot;):
          #print('Initial XML file setting :', elm.attrib)
          if elm.attrib == {'type': 'integer', 'value': '1'}:
              print('unit SET TO 1 (BINARY)')
              isBinary = True
          elif elm.attrib == {'type': 'integer', 'value': '0'}:
              print('unit SET TO 0 (GREYSCALE)')
              isBinary = False
      return isBinary     
   
  
  
  def xml_to_bin(xmlfile_path):
      tree = ET.parse(xmlfile_path)
      root = tree.getroot()
      for elm in root.findall(&quot;./Common/ForceToBinary&quot;):
          if elm.attrib == {'type': 'integer', 'value': '0'}:
              time.sleep(1.0)
              elm.attrib = {'type': 'integer', 'value': '1'}
              time.sleep(1.0)
      tree.write(xmlfile_path, xml_declaration=True, method='xml', encoding='UTF-8')
      print('xml file has had USB/Common/ForceToBinary value changed to BINARY.')
  
  
  
  def xml_to_bin2(xmlfile_path):
      tree = ET.parse(xmlfile_path)
      root = tree.getroot()
      for elm in root.findall(&quot;./Common/ForceToBinary&quot;):
          if elm.attrib == {'type': 'integer', 'value': '0'}:
              time.sleep(1.0)
              elm.attrib = {'type': 'integer', 'value': '1'}
              time.sleep(1.0)
          with open(xmlfile_path, 'w') as filey:
              tree.write(filey, xml_declaration=True, method='xml', encoding='UTF-8')
  
  #USING THE FUNCTIONS
  if __name__ == &quot;__main__&quot;:
      xmlfile_path = (r'C:\...\USB_TEST_04.XML')
      is_unit_binary = get_bin_gs_xml_setting(xmlfile_path)
      print(&quot;Is unit binary : &quot;, is_unit_binary)
      if is_unit_binary is False:
          xml_to_bin2(xmlfile_path)
      time.sleep(1)
      print(get_bin_gs_xml_setting(xmlfile_path))
  
      is_unit_binary = get_bin_gs_xml_setting(xmlfile_path)
      print(&quot;Is unit binary : &quot;, is_unit_binary)
  </code></pre>
  <p><strong>xml file</strong></p>
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt;
  &lt;USB&gt;
    &lt;Common&gt;
      &lt;ForceToBinary type=&quot;integer&quot; value=&quot;0&quot; /&gt;
      &lt;GuardValue type=&quot;integer&quot; value=&quot;0&quot; /&gt;
    &lt;/Common&gt;
  &lt;/USB&gt;
  </code></pre>
  <p><strong>Traceback when running xml_to_bin2</strong></p>
  <pre><code>The script terminated with an unhandled error.
  
  TypeError: write() argument must be str, not bytes
  File line...
      xml_to_bin2(xmlfile_path)
  File xml_to_bin2...
      tree.write(filey, xml_declaration=True, method='xml', encoding='UTF-8')
  </code></pre>