def remove_last_xmltag_in_file(fname, tag=None):
 fh = open(fname, 'r+')
 i = (-1)
 last_tag = []
 while True:
  fh.seek(i, 2)
  char = fh.read(1)
  if (not char.isspace()):
   last_tag.append(char)
  if (char == '<'):
   break
  i -= 1
 last_tag = ''.join(last_tag[::(-1)])
 last_tag = last_tag.rstrip('>').lstrip('</')
 if ((tag is not None) and (tag != last_tag)):
  etxt = ('The given xml tag (%s) was not the last one in the file' % tag)
  raise RuntimeError(etxt)
 i -= 1
 while True:
  fh.seek(i, 2)
  char = fh.read(1)
  if ((not (char == ' ')) and (not (char == ' DCTB '))):
   break
  if (fh.tell() == 1):
   break
  i -= 1
 fh.truncate()
 fh.close()
 return last_tag

def remove_last_xmltag_in_file ( fname , tag = None ) : fh = open ( fname , 'r+' ) i = ( - 1 ) last_tag = [ ] while True : fh . seek ( i , 2 ) char = fh . read ( 1 ) if ( not char . isspace ( ) ) : last_tag . append ( char ) if ( char == '<' ) : break i -= 1 last_tag = '' . join ( last_tag [ : : ( - 1 ) ] ) last_tag = last_tag . rstrip ( '>' ) . lstrip ( '</' ) if ( ( tag is not None ) and ( tag != last_tag ) ) : etxt = ( 'The given xml tag (%s) was not the last one in the file' % tag ) raise RuntimeError ( etxt ) i -= 1 while True : fh . seek ( i , 2 ) char = fh . read ( 1 ) if ( ( not ( char == ' ' ) ) and ( not ( char == ' DCTB ' ) ) ) : break if ( fh . tell ( ) == 1 ) : break i -= 1 fh . truncate ( ) fh . close ( ) return last_tag
given an xml file name and a tag .